我为嵌套模型的控制器语法而苦苦挣扎。还有很多其他问题,但我没有任何运气,我已经达到了 2 天的挣扎门槛,所以是时候问了!
我有 3 个模型,用户、农场、产品。- 用户 has_many 农场,农场 has_many 产品。
在用户显示页面上,我部分显示了用户创建的农场列表...
#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 %>
这按预期工作。
我想做的是在_farm部分中显示农场的所有产品作为列表。但是我不知道要向控制器添加什么?添加
@products = Product.find(params[:id])
显然展示了所有产品,而不仅仅是那个农场生产的产品。那么如何创建控制器代码和/或部分代码以仅显示该特定农场创建的产品?
我在想……
@products = Product.find(params[:farm_id])
但它不起作用,似乎很简单!
我显然需要将密钥传递给局部中的局部,但我不知道该怎么做!
任何帮助将不胜感激......它让我发疯!
非常感谢,亚历克斯
编辑 ::
#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
编辑 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>