【问题标题】:nested_attributes with simple_field_for getting unpermitted parameters error rails带有 simple_field_for 的nested_attributes 获取未经许可的参数错误 rails
【发布时间】:2014-03-20 19:56:31
【问题描述】:

我有模型

class Profile < ActiveRecord::Base
  belongs_to :user
  has_many :user_interests
  has_many :interests, :through => :user_interests
  accepts_nested_attributes_for :user_interests
end

class UserInterest < ActiveRecord::Base
  belongs_to :profile
  belongs_to :interest
end

class Interest < ActiveRecord::Base
  has_many :user_interests
  has_many :profiles, :through => :user_interests
end

controller

private
    def profile_params
      params.require(:profile).permit(:first_name, :last_name, :date_of_birth, user_interests_attributes: {:interest => []})
    end

查看

=#"http://stackoverflow.com/questions/18428871/multi-column-forms-with-fieldsets"
= simple_form_for(@profile, html: {class: 'form-horizontal'}) do |f|
  .well.well-lg
    %fieldset
      %legend Personal Information
      .row
        .col-sm-4
          .form-group
            = f.input :first_name, label: 'First Name*'
            = f.hint 'No special characters, please!'
        .col-sm-4
          .form-group
            = f.input :last_name, label: 'First Name*'
            = f.hint 'No special characters, please!'
      .row
        .col-sm-4
          .form-group
            = f.input :date_of_birth, as: :string, 'data-behaviour'=>'datepicker'
      %legend Other Information
      .row
        .col-sm-4
          .form-group
            = f.simple_fields_for :user_interests, UserInterest.new do |s|
              = s.collection_select(:interest, Interest.all, :id, :name,{},{:multiple=>true})
            = f.hint 'Please use Ctrl key on your keyboard to select multiple items'
      .row
        = f.submit
      

但得到错误未允许的参数

profile_params

不允许的参数:兴趣

=> {"first_name"=>"", "last_name"=>"", "date_of_birth"=>"", "user_interests_attributes"=>{"0"=>{}}}

我的参数在哪里:

参数 => {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"7/7sKljbi88cmUOen/WFWzhzV6exE8I8fBnNMA5EELw=", "profile"=> {"first_name"=>"", "last_name"=>"",
"date_of_birth"=>"",
"user_interests_attributes"=>{"0"=>{"interest"=>"test"}}}, "commit"=>"更新配置文件", "action"=>"update", "控制器"=>"配置文件", "id"=>"1"}

请指正我错的地方

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 nested-attributes


    【解决方案1】:

    您忘记在 UserInterest 上添加 accepts_nested_attributes_for :interest

    class Profile < ActiveRecord::Base
      belongs_to :user
      has_many :user_interests
      has_many :interests, :through => :user_interests
      accepts_nested_attributes_for :user_interests
    end
    
    class UserInterest < ActiveRecord::Base
      belongs_to :profile
      belongs_to :interest
      accepts_nested_attributes_for :interest
    end
    
    class Interest < ActiveRecord::Base
      has_many :user_interests
      has_many :profiles, :through => :user_interests
    end
    

    您可能必须将兴趣定义为控制器中 strong_parameters 定义的有效参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 2021-05-03
      相关资源
      最近更新 更多