English 中文(简体)
Rendering other controllers templates (Ruby on Rails)
原标题:

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.

最佳回答

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.

问题回答

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

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.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签