gpt4 book ai didi

ruby-on-rails - 将嵌套属性与设计一起使用时的质量分配警告

转载 作者:行者123 更新时间:2023-11-30 23:50:00 25 4
gpt4 key购买 nike

我在嵌套属性和设计方面遇到了麻烦。与 How do I use nested attributes with the devise model 类似的问题.
据我所知,我已经按照此处的建议进行了所有设置:
Override devise registrations controller

我已经为用户和订阅设置了关联,我有 "accepts_nested_attributes_for" ,并包括 :subscriptions_attributesattr_accessible ,但我从设计 Controller 收到警告。

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :first_name, :last_name
has_many :subscriptions
accepts_nested_attributes_for :subscriptions
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :subscriptions_attributes
...
end

——
class Subscription < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id, :chargify_subscription_id, :chargify_product_handle
attr_accessible :user_id, :chargify_subscription_id, :chargify_product_handle
...
end

设计/注册/new.html.erb :
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :first_name %><br />
<%= f.text_field :first_name %></p>
...

<%= f.fields_for :subscription do |s| %>
<p><%= s.label :chargify_subscription_id %><br />
<%= s.text_field :chargify_subscription_id %></p>
...

我收到以下警告:
Started POST "/users" for 127.0.0.1 at Sat May 14 12:38:49 -0700 2011
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"commit"=>"Sign up", "authenticity_token"=>"wNZhZgIhYm9CpZfhvDiRBqaJseoO8QvR0Mk9VIybhcI=", "utf8"=>"✓", "user"=>{"password_confirmation"=>"[FILTERED]", "last_name"=>"9", "subscription"=>{"chargify_product_handle"=>"medium", "chargify_subscription_id"=>"123"}, "password"=>"[FILTERED]", "first_name"=>"9", "email"=>"99@99. com"}}
WARNING: Can't mass-assign protected attributes: subscription

我试过使用 subscription_attributes (单数)在 attr_accessible但这不起作用。

对我可能做错的事情有什么建议吗?
谢谢。

最佳答案

由于您的 User 和 Subscription 模型之间有 has_many 关联,我相信您需要指定 fields_for :subscriptions而不是 fields_for :subscription .

<%= f.fields_for :subscriptions do |s| %>
<p><%= s.label :chargify_subscription_id %><br />
<%= s.text_field :chargify_subscription_id %></p>
...

然后 fields_for 范围内的属性将使用 subscriptions_attributes 参数传递,该参数应该可以工作,因为您有 attr_accessible :subscriptions_attributes .

有关嵌套 has_many 关联形式的更多示例,请查看 Rails documentation 的“嵌套属性示例”部分(一对多)。 .

关于ruby-on-rails - 将嵌套属性与设计一起使用时的质量分配警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6005086/

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