0

I an struggling with the syntax of the controller for nested models. There are many other questions out there but i am not having any luck and i have reached my 2 day struggling threshold, so its time to ask!

I have 3 Models, User, Farm, Product. - User has_many Farms, Farm has_many Products.

On the user show page i display a list of farm/s created by the user in a partial...

#users_controller
def show
    @user = User.find(params[:id]) 
    @farms = @user.farms   
  end

#show.html.erb
 <% if @user.farms.any? %>
   <center><h4>My Farms(<%= @user.farms.count %>)</h4></center>
   <hr>
<%= render @farms %>

This works as expected.

What i would like to do is display all the products of the farm, within the _farm partial, as a list. However I dont know what to add to the controller? Adding

@products = Product.find(params[:id])

obviously displays all products, not just one made by that farm. So how to i create the controller code and/or the partial code to only show the products created by that particular farm?

I was thinking along the lines of...

@products = Product.find(params[:farm_id])

but it does not work and seems to simple!

I obviously need to pass a key somewhere to the partial within the partial, but i have no idea how to do it!

Any Help will be massively appreciated...its driving me nuts!

Many thanks, Alex

EDIT ::

#product.rb
class Product < ActiveRecord::Base
  attr_accessible :description, :farm_id, :name, :ammount, :price, :category, :pic, :longitude, 
  :latitude, :image

  belongs_to :farm

 mount_uploader :image, ImageUploader


#farm.rb
class Farm < ActiveRecord::Base

  attr_accessible :content, :name, :user_id, :description, :street_name, :bldg_name, :region, :post_code, 
                        :province, :contact_number, :swap, :organic, :deliver, :image, :products_attributes


  belongs_to :user
  has_many :products
 acts_as_followable
  validates :user_id, presence: true
accepts_nested_attributes_for :products
  mount_uploader :image, ImageUploader


#user.rb
class User < ActiveRecord::Base

  mount_uploader :avatar, ImageUploader
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :confirmable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :region, 
    :description, :avatar, :street_name, :bldg_name, :post_code, :province, :contact_number, :avatar, 
    :avatar_cache, :remove_avatar, :products_attributes, :product

  # attr_accessible :title, :body

  acts_as_follower

  has_many :farms
  has_many :swaps
  has_many :products, :through => :farms

  # validates_presence_of   :image
  # validates_integrity_of  :image
  # validates_processing_of :image

  has_private_messages

EDIT 2 ::

#_products.html.erb

<hr>
<div class="row-fluid">

      <div class="span3"> <%= image_tag product.image_url(:thumb) %> </div>  

      <div class="span5"> 
         <h2><%= product.name %></h2>
         <table>
<tr>
<td><strong>Ammount Available ::</strong></td>
<td><center><%= product.ammount %></center></td>
<td><strong>Kilos</strong></td>

</tr>
<tr>
<td><strong>Price/Kilo ::</strong></td>
<td><center><%= product.price %></center></td>
<td><strong>Euros</strong></td>
</tr></table>
      </div>     
      <br><br><br>
      <div class="span3">   
<%= link_to "View Product", product, class: "btn btn-small btn-secondary"  %>
<% if correct_user?(@user) %>
  <%= link_to "delete", product, class: "btn btn-small btn-secondary", method: :delete, confirm: "You sure?" %><% end %>


      </div> 

-

#_farm.html.erb
<div class="<%= cycle("even", "odd") %>">
<div class="container">

<div class="row-fluid">
  <div class="span2">
    <%= image_tag farm.image_url %>
  </div>

  <div class="span2">
    <%= farm.name %>
  </div>

  <div class="span4">
    <span><%=  "   Location  ::  " %><%= farm.user.region %></span><br>
    <span><%=  "   Can it be delivered  ::  " %><%= farm.deliver ? 'No' : 'Yes' %></span><br>
    <span><%=  "   Is it Organic  ::  " %><%= farm.organic ? 'Yes' : 'No'  %></span><br>
    <span class="timestamp">Listed on <%= farm.created_at.strftime("%d %b. %Y") %> </span>


  </div>
  <div class="span4">
<br>
<br>
<br>

<%= link_to 'Sell A Product', new_farm_product_path(farm), class: "btn btn-small btn-secondary" %> 
<%= link_to 'Edit Grow Spot', farm, class: "btn btn-small btn-secondary" %> 
  </div>         
 </div><!-- end container -->  
</div>

<div class="row-fluid">
  <div class="span2"></div>
  <div class="span8">

<% for product in farm.products do %>
  <%= render :partial => "product", :product => product %>
<% end %>

  </div>
  <div class="span2"></div>
2
  • show your models code, if everything is right there you should write farm.products and have all the products for each farm Jun 25, 2013 at 13:15
  • thanks @rmagnum2002. Have listed Model code. How would you render farm.products as a partial? I have not tried putting it in a block to iterate over all products, although maybe worth a try. Looking to renter the partial because of the HTML prettyness!, rather than just a list of text. Jun 25, 2013 at 13:52

1 Answer 1

0

Using <%= render @farms %> in a view means you must have a partial called _farm.html.erb in app/views/farms

in that partial you can use farm variable:

<% for product in farm.products do %>
  <%= product.name %>
  <%= product.price %>
  .... 
<% end %>

no need to add something in controller.

update

<% for product in farm.products do %>
  <%= render :partial => "product", :locals => { :product => product } %>
<% end %>

in app/views/farms/_product.html.erb

<div class="zzz">
   <div><%= product.price %></div>
</div>
7
  • Ahh, Genius! Let me give it a shot! Jun 25, 2013 at 13:53
  • Thanks @rmagnum2002 That works, and is a step closer, but is giving just a list as you would expect. Can i make it look nicer but getting some HTML in there? Like a partial? Jun 25, 2013 at 14:01
  • Hey @rmagum2002, That is exactally what i was looking for, but it does not work! First off it did not find the template, but i moved it to /views/users/ and was fine. But now its giving 'undefined method `name' for nil:NilClass'. The code works, but when its in a partial it does not! Will add the partial code to the question incase there is a fault there. Jun 25, 2013 at 14:30
  • I have updated the code.. rendering a partial with locals was the part I hated the most. I am never sure how and when to use it :) Jun 25, 2013 at 14:39
  • guides.rubyonrails.org/… 3.4.4 Passing Local Variables tells you how to send variables to partials, but they use the case with a instance variable @zone and not zone so I am not sure it will work for you. In my app in a case like yours I just quit using partials. Jun 25, 2013 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.