views:

371

answers:

4

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 apparently 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.

A: 

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.

Peter
Thanks! Alright,that sounds better. But is it a good idea to make a partial for every model I want to include in other model's pages? My main question concers the design, not how to call render.
FrontierPsycho
Actually, on closer inspection, your suggestion requires that I change my partial to operate on single items, I believe, while currently it takes a collection and iterates over it. I tried it and it didn't work. Do you think it's better that I change the code like that?
FrontierPsycho
I think you should have an individual supermarket view. *But* you don't have to replace your current "multiple" view. Instead, you can use your single view in the "multiple" version. And then you can use both on your code.
egarcia
A: 

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.

Dan McNevin
The problem is that _supermarket.html.erb belongs to the supermarket model and the page I am trying to render it into, to another (supermarketchains) and thus they are under different folders. I tried your suggestion and I got an error, Rails was telling me it couldn't find "supermarketchains/_supermarket.html.erb", which is natural, because the file is in supermarkets/_supermarket.html.erb. Good point about moving code into the controller, though.
FrontierPsycho
A: 

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.

Rilindo
I've done that, but that could arguably be considered a case of repeating myself and I was looking for a way to avoid it :) I am just exploring if there is a "Rails" way of doing this, that is Approved and Well Tested.
FrontierPsycho
+1  A: 

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 %>
Ben Marini