I am building a sample site to familiarize myself with RoR. I followed the book "Agile Web Development with Rails" up to a point, and now I am experimenting and using it as a reference, however I haven't been able to find the answer to my problem.
I have two models of interest, one is supermarketchain and the other supermarket. Obviously, a supermarket chain has a bunch of supermarkets. What I am trying to do is get the basic "show" page for a single supermarket chain to display a list of the supermarkets that belong to it.
I also didn't want to repeat myself (because appa开发者_JAVA百科rently it's a Very Bad Thing), and so I thought I could use "render" to insert supermarket's index.html.erb into the supermarketchain/show.html.erb page, like this:
<%= render :template => 'supermarkets/index' %>
However, that produced zero output on the page.
My next approach was to make this partial:
<div id="supermarket-list">
<h1><%= I18n.t "supermarket.title" %></h1>
<table>
<% for s in @supermarkets %>
<tr class="<%= cycle('list-line-odd', 'list-line-even') %>">
<td>
<%= s.supermarketchain.name %>
</td>
<td>
<%= s.address %>
</td>
<td class="list-actions">
<%= link_to I18n.t("general.show"), s%> <br/>
<%= link_to I18n.t("general.edit"), edit_supermarket_path(s) %> <br />
<%= link_to I18n.t("general.delete"), s, :confirm => I18n.t("general.confirmation"), :method => :delete %>
</td>
</tr>
<%end%>
</table>
</div>
And then use:
<% @supermarkets = Supermarket.all %>
<%= render :partial => 'supermarkets/supermarket' %>
To insert it on the supermarketchain's show page.
What I am wondering is whether this is a good practice. It seems to me weird to initialize a variable for use by a partial, when what I want displayed is the exact result of the "index" action of the supermarkets controller. Comments?
Please ask for any needed clarifications.
It's perfectly fine in Rails to render partials from other controllers. Commonly if a partial doesn't really belong to any one particular controller, it is put in app/views/shared
. In this case it makes sense to keep the partial with the supermarket controller though I think.
Here's how you could make use of the same partial supermarkets/_supermarket.html.erb
for both sections. The partial would have the local variable supermarket
available.
# supermarketchain/show.html.erb
<%= render :partial => "supermarkets/supermarket", :collection => @supermarketchain.supermarkets %>
# supermarkets/show.html.erb
<%= render :partial => "supermarket", :object => @supermarket %>
You can, instead, do
<%= render :partial => 'supermarket', :collection => Supermarket.all, :as => :s %>
and the partial will be called once for every supermarket, with variable name s
.
Okay, so if I get this right, you have a list of chains and each chain has a list of supermarkets, right? Do you have an association setup between the two models? Because that would just simplify things if you have. In this case, assuming that you rename the model from "supermarketchains" to just "chain" to (clear any confusion). You can associate the models like so>
has_many :supermarkets
And in the supermarket model.
belongs_to :chain
From there, it's just an issue of looping like:
<% if chain.supermarkets.count > 0 %>
#Do stuff.
<% end %>
That said, if you haven't setup controllers to pull that info, it may take extra work.
I've struggled with this myself, but found that's it's not really supported as a way to render partials, and since I trust the Rails developers conventions usually, I decided that it probably wasn't a good idea.
I don't think using a template in there will work, you could maybe use render_to_string
, but this seems hackish, and could cause you more headache in the future.
Also, if you want to render a collection, the best way is to set the instance variable in the controller action, not in the view..
def index
@supermarkets = Supermarket.all
end
And then in the view index.html.erb
, call:
<%= render @supermarkets %>
Which will then iterate over every member of the array, and render views/supermarkets/_supermarket.html.erb
for each, with 'supermarket' being the current member available in the partial.
<%= supermarket.name %><br />
<%= supermarket.address %>
I worry about the performance of a lot of these renders, but just keep an eye on the logs and the times, I think the times of render vs. doing it manually in the original view are about the same.
精彩评论