gpt4 book ai didi

ruby-on-rails - 无法使用嵌套属性批量分配 protected 属性

转载 作者:行者123 更新时间:2023-12-02 00:06:48 25 4
gpt4 key购买 nike

我以前使用过嵌套属性和 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

app/views/tags/_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 %>

两个模型的参数都正常传递,但我在控制台中收到大量分配网络错误...

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'

感谢任何帮助!

最佳答案

在 tag.rb 中,使 skill 属性可访问。

class Tag < ActiveRecord::Base
has_many :skills
attr_accessible :name, :skills_attributes, :skill
accepts_nested_attributes_for :skills
end

关于ruby-on-rails - 无法使用嵌套属性批量分配 protected 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17883865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com