How to render a partial inside ActiveAdmin edit page - ruby-on-rails-3

How can I render a partial inside ActiveAdmin edit page?
Here is my code http://paste.ubuntu.com/1222340/
I want to render a page after the submit button.
How can I do this?

Try this
form do |f|
f.inputs do
f.input :name
end
f.actions
f.form_buffers.last << f.template.render('shared/test')
end

Related

ActiveAdmin bug? Nested form in show resource page does not display form submit button

I am using ActiveAdmin to build an article show page where new comments can be added using a nested form but facing a problem. Article has_many :comments and also accepts_nested_attributes_for :comments. It all works fine in Rails console but I can't get the form submit button to show up. Here is my code for customizing activeadmin's show page for Article:
show do |article|
attributes_table do
row :id
row :title
end
div do
semantic_form_for [:admin, article], builder: ActiveAdmin::FormBuilder do |f|
f.has_many :comments, allow_destroy: false, new_record: true, heading: false do |g|
g.input :body
end
f.actions
end
end
end
The nested form is displayed and works correctly but the submit button is not displayed (meaning that f.actions has no effect).
Workaround: I moved the Nested form to a view and i'm able to see the "Update" button. like
show do
attributes_table do
row :id
row :description
end
panel "Link Projects" do
render partial: 'project_links', locals: { id: company }
end
end

Display 'show' action in partial within the 'index' action's view - rails

I have a property search page (:controller => 'properties', :action => 'index') that consists of a right sidebar that has a search form which displays the search results below the form. When the user clicks on a property in the right sidebar I want to display the details of that property in the main area of the index page on the left.
Right sidebar is a partial called properties/_property.html.erb:
<%= form_tag properties_path, :method => 'get' do %>
<%= text_field_tag 'location', (params[:location]) %>
<%= select_tag(:max, options_for_select([['No Max', ""], ['$100,000', 100000], ['$200,000', 200000], etc %>
more search fields for baths beds etc
<%= submit_tag "Search", :name => nil %>
<% end %>
<% #properties.each do |property| %>
<%= link_to([property.Address,property.City].join(", "), {:action => 'show', :id => property.id}) %>
<li><strong><%= number_to_currency(property.Price, :precision => 0) %></strong></li>
etc etc
<% end %>
The only way I know how to show the property details is with the 'show' action, but that takes the user to a new page, for example localhost:3000/properties/1865. I've made the 'show' view with the same layout as the 'index' view and have made the right sidebar a partial (properties/_property.html.erb) which appears on both 'show' and 'index' so when the user clicks on a property in the right sidebar and goes to localhost:3000/properties/1865 the property details are displayed correctly in the main area and the right sidebar is on the right.
But because localhost:3000/properties/1865 is a different page than localhost:3000/properties/index the search form in the right sidebar has forgotten it's parameters which means the list of search results in the right sidebar has changed back to the default list of all properties.
How can I display the 'show' action within a partial on the index page so the user's search parameters are remembered by the form in the right sidebar? Or if I have to go to the 'show' page how can I make the right sidebar stay exactly as it is?
Any ideas greatly appreciated, just a suggestion in the right direction would be good, have spent all day trying to figure it out and have got nowhere, thanks
I have made the show view with the same layout as the index view and
have made the right sidebar a partial (properties/_property.html.erb)
which appears on both show and index.
Yes but this will only get you a similar layout for both the pages. You want the list to persist between different requests.
You basically have two options. use session to remember the list which I wont recommend.
Other is you use form :remote => true or ajax and update the page partially.
EDIT:
What version of rails you are using? Do u have jquery loaded in your application?
Follow this SO POST.
You might have to change your show action a bit.
respond_to do |format|
format.js {render :partial => 'property_details' ,:layout => false}
format.html
end
Create a partial for property details and just put body content here.
And link will look like
<%= link_to "link name", {:action => :show, :id => item_id}, :remote => true ,:html => {:class => 'links_product'} %>
Also to update the view you may use(make sure you have rails.js in your page):
$(document).ready(
function(){
$("a.links_product").bind("ajax:success",
function(evt, data, status, xhr){
$("#response").html(data); // in case data is html. (_*.html.erb)
}).bind("ajax:error", function(evt, xhr, status, error){
console.log('server error' + error );
});
});
Have a div with id response or any valid id. Done!

Display edit user link only when that user is logged in

I have a Users resource and I want to display the "edit" link in the show.html.erb files only when that user is logged in. See screenshots.
Here is a list of users. Notice in the top right that Roger Sterling is logged in.
But if I click on Don Draper, the show.html.erb file displays the "Edit" button. I only want that to appear for Roger Sterling, in this case.
I'm using Devise for authentication. Any ideas on how to accomplish this? Let me know if you need to see any available code.
You can check current_user and user is shown, if they are the same, the link 'Edit' will display. You can define a helper method like this:
def correct_user?(user)
user == current_user
end
In your show action of UsersController will have:
def show
#user = User.find(params[:id])
...
end
and in your view, you can check:
<% if correct_user?(#user) %>
<%= link_to 'Edit', #user %>
<% end %>
Ha. Ok I got it.
Here is the loop in the show.html.erb file:
<% if (#user.name == current_user.name) %>
<%= link_to 'Edit', edit_user_registration_path(#user) %>
<% end %>
And the helper method:
def correct_user
user == current_user
end
Thanks for the push in the right direction!

Insert a non-input row into a Formtasic form

I am using Formtastic 2.1.1 in Rails 3.2 (with Active Admin) and I want to insert a row into my form that does not have an input field present. Is this possible and what is the syntax in the Formtastic DSL to achieve this?
Here's an example:
form do |f|
f.inputs "Model Info" do
f.input :title
f.input :published
f.input :path
end
end
I'd like to do something like this:
form do |f|
f.inputs "Model Info" do
f.input :title
f.input :published
f.input :path
f.div "This is some important text that people using this form need to know"
end
end
Has anyone done this with Formtastic before?
Figured this out myself. I just needed to insert the html without any method calls, like so:
form do |f|
f.inputs "Model Info" do
f.input :title
f.input :published
f.input :path
end
f.inputs "Custom HTML Stuff" do
"<div id=\"element-name\">
Some kind of content
</div>".html_safe
end
end
To insert any custom code in any place, you may use f.form_buffers.last:
form do |f|
f.form_buffers.last << "<p>Hello world!</p>".html_safe # The simple way
ft = f.template # just a helper variable
f.inputs "Foo" do
f.input :title
f.form_buffers.last << ft.content_tag(:li) do
ft.content_tag(:p, "Hello again!") +
ft.tag(:input, type: :hidden, name: "bar[]", value: "baz")
end
f.input :path
end
end
Just be careful about the HTML structure. If you call this from f.inputs block, your code will be placed inside an <ol> element. On the "form" level, you are inside a <form> element.
A little warning: As with any "undocumented feature" this method may change without warning in any new release.
IDK if this is still something people search for but it was for me. Since form_buffers has been deprecated, I did the following and it worked out beautifully (with some CSS help).
form do |f|
f.inputs "Model Info" do
f.input :title
f.input :published
f.input :path
li "Your important text here", class: "some_class"
end
end
this outputs:
<li class="some_class">
Your important text here
</li>
Got this idea from the docs after a too long Google search here
FORMS and here ARBRE.
Here's a slightly simplified form of #arsen7's answer:
f.form_buffers.last <<
"<p>Activate interlock, dynatherms connected</p>".html_safe
which in my form looks like this:
And here's one that mimics ActiveAdmin's default style:
f.form_buffers.last << (<<END
<li class="string input optional stringish">
<label class="label">Activate interlock</label>
<div style="display: inline-block;">Dynatherms connected</div>
</li>
END
).html_safe
which looks like this:

Rails 3 - Unable to create a new post while at the SHOW view of another Post

I have a model named "Post". I want to use a modal form to create a new post while at the SHOW view of another post. Meaning while I am viewing the post named "John" in its show view, I would like to be able to create a new post from right there.
The problem I have is that the ID of the new post remains the same as the post I am viewing, and causes the update action to be fired instead of the create action. Any suggestions on how to handle this?
Build a new post with Post.new and use that in a form_for:
<%= form_for Post.new %>
<%= render "form" %>
<% end %>
Of course this means you'll need to remove the form_for from your form partial if you have it in there, but that's a small sacrifice to make.
However if you really don't want to do that then you will have to pass through a local variable to the form partial to indicate which post you want to display. On the show page you'd have this:
<%= render :partial => "form", :locals => { :post => Post.new } %>
In the new and edit views you'd do this:
<%= render :partial => "form", :locals => { :post => #post } %>
The line is a little bit longer, but that would allow you to keep the form_for tag inside the form partial and not clog up the three other views with it.