我以前使用过嵌套属性和 form_for,但只是缺少一些简单的东西。这是我的模型...
技能.rb
class Skill < ActiveRecord::Base
belongs_to :tag
attr_accessible :tag_id, :user_id, :weight
end
标签.rb
class Tag < ActiveRecord::Base
has_many :skills
attr_accessible :name, :skills_attributes
accepts_nested_attributes_for :skills
end
应用程序/视图/标签/_form.html.erb
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<%= f.fields_for :skill do |s| %>
<%= s.label :weight %><br />
<%= s.text_field :weight %>
<% end %>
两种模型的参数都可以通过,但是我在控制台中收到了一个 mass assignmentmnet 错误...
Started POST "/tags" for 127.0.0.1 at 2013-07-26 10:13:51 -0400
Processing by TagsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"yofQhmgOyNHvnws/Lg+BoS4TqeTwPdyQjQbLXotnEzI=", "tag"=>{"name"=>"test", "skill"=>{"weight"=>"ee"}}, "commit"=>"Create Tag"}
Completed 500 Internal Server Error in 1ms
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: skill):
app/controllers/tags_controller.rb:43:in `new'
app/controllers/tags_controller.rb:43:in `create'
任何帮助表示赞赏!