Note that there are some explanatory texts on larger screens.

plurals
  1. PORendering other controllers' templates (Ruby on Rails)
    text
    copied!<p>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. </p> <p>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. </p> <p>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: </p> <pre><code>&lt;%= render :template =&gt; 'supermarkets/index' %&gt; </code></pre> <p>However, that produced zero output on the page. </p> <p>My next approach was to make this partial: </p> <pre><code>&lt;div id="supermarket-list"&gt; &lt;h1&gt;&lt;%= I18n.t "supermarket.title" %&gt;&lt;/h1&gt; &lt;table&gt; &lt;% for s in @supermarkets %&gt; &lt;tr class="&lt;%= cycle('list-line-odd', 'list-line-even') %&gt;"&gt; &lt;td&gt; &lt;%= s.supermarketchain.name %&gt; &lt;/td&gt; &lt;td&gt; &lt;%= s.address %&gt; &lt;/td&gt; &lt;td class="list-actions"&gt; &lt;%= link_to I18n.t("general.show"), s%&gt; &lt;br/&gt; &lt;%= link_to I18n.t("general.edit"), edit_supermarket_path(s) %&gt; &lt;br /&gt; &lt;%= link_to I18n.t("general.delete"), s, :confirm =&gt; I18n.t("general.confirmation"), :method =&gt; :delete %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;%end%&gt; &lt;/table&gt; &lt;/div&gt; </code></pre> <p>And then use:</p> <pre><code>&lt;% @supermarkets = Supermarket.all %&gt; &lt;%= render :partial =&gt; 'supermarkets/supermarket' %&gt; </code></pre> <p>To insert it on the supermarketchain's show page. </p> <p>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? </p> <p>Please ask for any needed clarifications. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload