在 Devise 中使用嵌套属性时出现批量分配警告

发布于 2024-11-06 22:23:41 字数 2487 浏览 3 评论 0 原文

我在嵌套属性和设计方面遇到问题。与类似的问题如何将嵌套属性与设计模型。 据我所知,我已经按照此处的建议设置了所有内容: 覆盖设备注册控制器

我已经设置了用户和订阅的关联,我有“accepts_nested_attributes_for”,并在 attr_accessible 中包含了 :subscriptions_attributes,但我收到了来自 Devise 控制器的警告。

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

devise/registrations/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

我尝试在 attr_accessible 中使用 subscription_attributes (单数),但事实并非如此工作。

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

I'm having trouble with nested attributes and Devise. A similar issue to How do I use nested attributes with the devise model.
As far as I can tell I've got everything set up as recommended here:
Override devise registrations controller

I've set up the associations for Users and Subscriptions, I have "accepts_nested_attributes_for", and included the :subscriptions_attributes in attr_accessible, but I get a warning from the Devise 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

devise/registrations/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>
            ...

I'm getting the following warning:

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

I've tried using subscription_attributes (singular) in attr_accessible but that doesn't work.

Any suggestions for what I might be doing wrong?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请爱~陌生人 2024-11-13 22:23:41

由于您的用户和订阅模型之间存在 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>
        ...

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

有关嵌套 has_many 关联形式的更多示例,请查看 Rails 文档

Since you have a has_many association between your User and Subscription model, I believe you need to specify fields_for :subscriptions rather than fields_for :subscription.

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

The attributes within the fields_for scope will then be passed using the subscriptions_attributes parameter, which should work since you have attr_accessible :subscriptions_attributes.

For more examples of nested has_many association forms, check out the 'Nested Attributes Examples' section (one-to-many) of the Rails documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文