Note that there are some explanatory texts on larger screens.

plurals
  1. POwhat should be the way to pass multiple activerecord object from controller to views?
    primarykey
    data
    text
    <p>I will give an example.</p> <p>Say I have Category and Product as my two models and another ChosenProduct that specifies/handles the many to many relationship between them. Now when I have certain categories and I am iterating in a loop on these categories getting the corresponding products. How to access these multiple product objects in my view? </p> <p>I am new to Rails and MVC so for now I am doing this task in my view itself. But I think this shouldn't be the right way. How should I approach this ?</p> <p>I am adding my view code as asked. This works for me fine now. But I don't think its pretty.</p> <pre><code> &lt;ul&gt; &lt;% @categories.each do |category| %&gt; &lt;li&gt; &lt;%= image_submit_tag("add.png", :height =&gt; "20", :width =&gt; '20', :name=&gt;"add_product_to_#{category.id}", :id=&gt;"add_product_to_#{category.id}" ) %&gt; &lt;%= category.category_name %&gt; &lt;% @chosen_products = category.chosen_products %&gt; &lt;% @chosen_products.each do |chosen_product| %&gt; &lt;% @products = Product.where(:id =&gt; chosen_product.product_id).all %&gt; &lt;% @products.each do |product| %&gt; &lt;ul&gt; &lt;li&gt;&lt;%= product.product_name %&gt; &lt;ul&gt; &lt;li&gt;&lt;%= image_tag("#{product.product_image_url}", :size =&gt; "200x200", :alt =&gt; "Can not load image!") %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= product.product_image_url %&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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