【问题标题】:rails nested form not permitting a parameter that it shouldrails嵌套形式不允许它应该的参数
【发布时间】:2014-01-11 00:39:16
【问题描述】:

我不明白为什么这个嵌套表单不起作用。 Skill belongs_to Profile 和 profile has_many 技能。由于某种原因,提交表单时服务器日志显示以下错误:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"gdskfkjsdflkdjshgk",
 "profile"=>{"skill"=>{"label"=>"PHP"}}, "commit"=>"ADD +", "id"=>"134"}

Unpermitted parameters: skill

表格代码如下:

<%= form_for @profile  do |f| %>            
        <%= f.fields_for :skill do |builder| %>
               <%= builder.select :label, @crafts.collect {|craft| craft.label},
                  {include_blank: true} %>
        <% end %>   

        <%= f.submit "ADD +", :id => "clickme", :class => 'button button-add' %>
<% end %>

以下是配置文件控制器中的强参数:

params.require(:profile).permit( skill_attributes: [:label, :id])   

这是表单操作的编辑控制器:

def edit
    @profile = current_user.profile 
    @skill = Skill.new  #field for new skill
end

这是配置文件模型的相关部分:

class Profile < ActiveRecord::Base
    has_many :skills
    accepts_nested_attributes_for :skills,  :allow_destroy => true
end

我使用“skill_attributes”和“f.fields_for :skill”的原因是因为我只希望一次创建/更新一项技能,并且使用复数形式在页面上放置两个输入。

【问题讨论】:

  • 这方面的最新消息是什么?
  • 如果 OP 不打算更新所提供的任何解决方案是否有用,我将不得不否决这个问题。

标签: ruby-on-rails ruby-on-rails-4 nested-forms nested-attributes strong-parameters


【解决方案1】:

您的表单(技能)与 Accepts_nested_attributes (skills_attributes) 的预期不匹配。我认为你可以使用它:

= fields_for "profile[skills_attributes]" do |builder|

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多