Quick rails question. When I do this:
<%= render #users %>
Rails will search for
_user.html.erb
Is there option to render different file?
EDIT
I do not made custom partial - I use params[:action] to determine what kind of html I will render for partial.
Yes, there is.
<%= render partial: 'path/to/partial', collection: #users %>
Read more about this on Rails Guides.
Rails looks for a method on the model called to_partial_path to determine which partial to render. So you can override this method to define your own partial within the model logic. See #4 in this post: http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2/
Related
I've tried to make sense of layouts but got lost..Also googled & looked at other similar questions on SO but none of them helped.
Say I have MVC's (scaffold'ed) for A and B, creating their ..views/A/index.html.erb and ..views/B/show.html.erb among the rest.
A's index method sets a #a_collection.
Within B's show view I want to:
<p>..stuff for B..</p>
<%= render A's index %>
<p>..some more B-stuff</p>
How can I render A's index in that place in B's show?
You don't typically render a view inside of another view. You use partials to share code across views. For example:
# app/views/products/_product.html.erb
# this is the code you want to reuse
<p>Product Name: <%= product.name %></p>
# app/views/products/index.html.erb
<%= render #products %>
# app/views/stores/show.html.erb
<h1><%= #store.name %></h1>
<h2>Our Products</h2>
<%= render #store.products %>
<%= render #products %> is shorthand for <%= render partial: "product", collection: #products %>.
This is just the implicit rendering - in many cases you'll want to add more partials and render them explicitly. Like for example the _form.html.erb partial that you'll find in the scaffolds thats used to share a form between the create and edit views.
Think of partials like the view equivilent to a function - ideally they should take some input in the form of locals and result in a chunk of HTML.
Simple example:
index.html.erb:
<% #posts.each do |post| %>
<%= post.title %>
<% end %>
I can refactor this and move content to the _post.html.erb partial:
index.html.erb:
<%= render #posts %>
_post.html.erb:
<%= post.title %>
So how Rails passes attributes of every post without creating a block?
My posts_controller index action has only #posts = Post.all defined.
I thought that maybe by the name of the partial (post), but looks like its another Rails convention (plural and singular naming). Is it?
Many thanks for sharing!
Rails determines the name of the partial to use by looking at the model name in the collection. The full syntax to render a collection is as follows:
<%= render partial: "post", collection: #posts %>
However there is a shorthand for this:
<%= render #posts %>
According to docs:
When a partial is called with a pluralized collection, then the
individual instances of the partial have access to the member of the
collection being rendered via a variable named after the partial. In
this case, the partial is _post, and within the _post partial, you can
refer to post to get the instance that is being rendered.
You may find detailed explanation in rails guides
Is is possible to render a partial as a layout?
Currently, I have a partial _show.html.erb under app/views/users. In some other controllers, I tried to include layout 'users/show' to use that partial as a layout.
Yet, by default, Rails seems to be looking for layouts in layouts/ directory. And I get such error as:
Template is missing
Missing template layouts/users/show ....
Any suggestions?
I think that if you just want to include a partial in another view you should user render:
<%= render partial: 'users/show' %>
Perhaps in your other controller's action you have:
#user = #some_object.user
You'll need to pass in the #user to your partial. You might want to refer to a local variable, user, in your users/show partial and pass in the instance variable:
<%= render partial: 'users/show', locals: {user: #user} %>
You can also use the partial and layout options to together so that a partial will render inside a specified layout. For example, you could specify that your
<%= render partial: "contact_info", layout: "users/show" %>
This would tell the contact_info partial to render inside a layout found in app/layouts/users/show.html.erb.
I have a view that has a block corresponding to a partial view (very simple).
<td WIDTH ="70%">
<%= render "partial_1" %>
</td>
Now, When the user clicks certain button, I make a Ajax call to my controller, and after my business logic is done, I want to return to the same view but rendering a different partial view.
def ajax_call
....
render :layout => "administracion"
end
I tried changing my <%= render "partial_1" %> for <%= yield %>
and in my controller: render :layout => "administracion", :partial => "mypartial"
but when I do this, only the partial is rendered, the other elements of my original view are lost.
What should I do?
I'm using rails 2.3.9
Thank you!
Look at the example here.
In a few words, you need to create a js.erb view and include your partial this way:
$('#your_container').html('<%=escape_javascript render("your_partial") %>');
I want to create several link_to_remote links that are campaign names:
<% #campaigns.each do |campaign| %>
<!--link_to_remote(name, options = {}, html_options = nil)-->
<%= link_to_remote(campaign, :update => "campaign_todo",
:url => %>
<% end %>
I want the output to update on the page to render a partial, which runs a loop through the values associated with the campaign.
The API docs says this will render a partial, but I'm not clear where the name of the :partial template is passed in, either here or in the controller
Thanks.
The controller would render the partial, according to the docs.
Usually, the result would be a partial prepared by the controller with render :partial.
of course in the controller. Here you create an AJAX snippet that will pull an url you specify from your controller.
generated javascript has no access to any partials b/c it runs on client PC.
and controller decides WHAT to respond to this request. it can render a partial, a template, a text, or anything.