Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If the way you're displaying products between the admin section and the store section is constant except for the admin links (Create, Edit, Destroy), then I think it would be easiest to create a partial for your product. I assume you have a way of telling whether the user is an admin or not (I'll just use admin? for simplicity below). Inside your partial you do something like this...</p> <pre><code>&lt;div class="product"&gt; &lt;div class="productheader"&gt; &lt;%=h product.title %&gt; &lt;/div&gt; &lt;div class="productdescription&gt; &lt;%=h product.description %&gt; &lt;/div&gt; &lt;% if admin? %&gt; &lt;div class="productadmin"&gt; &lt;%= link_to "Delete", destroy_product_url %&gt; &lt;%= link_to "Edit", edit_product_url %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p>Be sure to name this partial _product.html.erb (the underscore tells rails that the template is a partial). Create a folder in the app/views directory of your application called shared and store the partial there. </p> <p>To render this partial in your other views, simply call the render method and pass the partial parameter. </p> <p>A single product:</p> <pre><code>&lt;%= render(:partial =&gt; "shared/product", :object =&gt; @a_product) %&gt; </code></pre> <p>Multiple products:</p> <pre><code>&lt;%= render(:partial =&gt; "shared/product", :collection =&gt; @products) %&gt; </code></pre> <p>Layouts can be applied to partials by adding the layout parameter. Partial layouts must be prefixed with the underscore but stored in the app/views directory associated with the controller. </p> <pre><code>&lt;%= render(:partial =&gt; "shared/product", :object =&gt; @a_product, :layout =&gt; "somelayout" %&gt; </code></pre>
 

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