我有两个模型,成分和食物:
class Food < ActiveRecord::Base
belongs_to :user
has_many :ingredients
accepts_nested_attributes_for :ingredients
attr_accessible :name, :price
end
class Ingredient < ActiveRecord::Base
belongs_to :user
belongs_to :food
attr_accessible :ingredient_name, :quantity_used
end
两种模型的架构如下:
create_table "foods", :force => true do |t|
t.string "name"
t.integer "user_id"
t.float "price"
t.string "ingredient_name"
end
create_table "ingredients", :force => true do |t|
t.string "ingredient_name"
t.integer "user_id"
t.integer "food_id"
t.integer "quantity_used"
end
我正在为 Food 创建一个表单,它还创建/更新成分表。我的表单正常工作,提交按钮更新了正确的属性,但我在每个表中还有其他我想更新的属性。例如,在食品控制器中,我想做类似ingredient.user_id = current_user.id
. 我知道我可以通过 params[:food] 访问内容,但是如何访问表单未更新的各个字段?
现在,我的表格是:
<%= form_for(@food) do |f| %>
<div class="field">
<%= f.label :Name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.number_field :price %>
</div>
<div>
<%= f.fields_for :ingredients do |builder| %>
<%= builder.label "Ingredient Used:" %>
<%= builder.text_field :ingredient_name %><br />
<%= builder.label "Quantity Used:" %>
<%= builder.text_field :quantity_used %><br />
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
如何在 Food#create 操作中访问此特定食品的成分?
谢谢!