【问题标题】:HABTM associantion: Unpermitted parametersHABTM 关联:不允许的参数
【发布时间】:2013-11-07 19:51:52
【问题描述】:

大家怎么了!

我一直在尝试在我的 rails (4) 应用程序中实现关联 has_and_belongs_to_many。这是我的代码:

我的类别模型

class Admin::Category
    include Mongoid::Document
    ...
    has_and_belongs_to_many :estimates, class_name: "User::Estimate", :inverse_of => :categories
end

我的估计模型

class User::Estimate
    include Mongoid::Document
    ...
    has_and_belongs_to_many :categories, class_name: "Admin::Category", :inverse_of => :estimates
end

强参数

def user_estimate_params
    params.require(:user_estimate).permit({:category_ids => []}, :favorite_time, :city_id, :number_of_guests, :event_date, :stage_where_it_is, :event_type, :observation, user_attributes:[:name, :email, :receive_news, :password, :password_confirmation, :preferred_phone, :alternate_phone])
end

我的请求

Parameters: {"utf8"=>"✓", "authenticity_token"=>"JJk6m+5VV7tCLDeUCRkQatQOw/fFiw8LCV39casua+c=", "user_estimate"=>{"user_attributes"=>{"email"=>"user@email.com"}, "city_id"=>"523b638b5383de1887000001", "event_type"=>"Casamento", "number_of_guests"=>"456", "event_date"=>"21/11/2013", "stage_where_it_is"=>"Nos próximos 30 dias", "category_ids"=>["523b667687924d211527530e", "523b667b87924d2115275317"], "observation"=>""}, "event_type_select"=>"527ace4353455263c9000000", "commit.x"=>"149", "commit.y"=>"40"}
Unpermitted parameters: category_ids

如您所见,我收到错误 Unpermitted parameters: category_ids,这使我无法保存我的类别,即使类别已添加到强参数方法。 p>

我正在使用 MongoDB/Mongoid。

我发现了很多类似的问题,但这些问题已经解决了添加 => []

到强参数方法,就像我已经做过的那样。

有什么帮助吗?

干杯

【问题讨论】:

  • 查看你得到的 params[:user_estimate] 哈希值。我相信 {:category_ids => []} 应该只是 :category_ids => []

标签: ruby-on-rails-4 mongoid has-and-belongs-to-many strong-parameters


【解决方案1】:

我有同样的问题和建议 字段:category_ids,类型:数组

没用。但是

params.require(:model).permit(category_ids: [], ... )

做到了。

【讨论】:

  • 必须做 permit({category_ids: []}, (add subhash) 才能完成这项工作
【解决方案2】:

已解决

我会说我需要做一个解决方法。我手动创建了一个字段,该字段应该在使用 HABTM 关联时自动创建。在我的 Estimate Model 中创建了该字段:

class User::Estimate
    include Mongoid::Document
    ...
    has_and_belongs_to_many :categories, class_name: "Admin::Category", :inverse_of => :estimates
    field :category_ids, type: Array
end

PS:字段声明必须在 HABTM 关联之后声明。

希望对你有帮助!

干杯!

【讨论】:

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