我正在为嵌套模型的控制器语法而苦苦挣扎。还有很多其他的问题,但我没有运气,我已经达到了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 partial中显示服务器场的所有产品作为列表。但是我不知道要添加到控制器中的内容?添加
@products = Product.find(params[:id])
显然显示所有产品,而不仅仅是该农场生产的产品。那么如何创建控制器代码和/或部分代码只显示该特定服务器场创建的产品?
我正在考虑......
@products = Product.find(params[:farm_id])
但它不起作用,似乎很简单!
我显然需要将某个键传递给局部内部的部分,但我不知道该怎么做!
任何帮助都将受到大力赞赏......它让我疯狂!
非常感谢,Alex
编辑::
#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>
答案 0 :(得分:0)
在视图中使用<%= render @farms %>
表示您必须在_farm.html.erb
中有一个名为app/views/farms
的部分
在那个部分你可以使用farm
变量:
<% for product in farm.products do %>
<%= product.name %>
<%= product.price %>
....
<% end %>
无需在控制器中添加内容。
<强>更新强>
<% for product in farm.products do %>
<%= render :partial => "product", :locals => { :product => product } %>
<% end %>
app/views/farms/_product.html.erb
中的
<div class="zzz">
<div><%= product.price %></div>
</div>