简体   繁体   English

Rails 3具有has_many的accepts_nested_attributes_for:through关系

[英]Rails 3 accepts_nested_attributes_for with has_many :through relationships

I'm building a user management system, and I'm starting off with a device for authentication. 我正在建立一个用户管理系统,并从一种用于身份验证的设备开始。

The system will allow users to have their own profile, as well as profiles for dependents (children). 该系统将允许用户拥有自己的个人资料,以及家属(孩子)的资料。 What I am envisioning is that when the users create their account, they are added to the users table (by the device) with the basic email/password fields. 我设想的是,当用户创建他们的帐户时,他们将通过基本的电子邮件/密码字段添加到用户表(由设备)。 I also want the users to have a profile in a seperate Profile model. 我还希望用户在单独的Profile模型中拥有一个Profile。 The thing that is different about my approach from other questions I have seen on StackOverflow is that I want to have a has_many relationship, :through a relationship table. 我的方法与我在StackOverflow上看到的其他问题不同的是,我希望通过关系表具有has_many关系。 Here's why. 这就是为什么。

Every user will have a profile, their own. 每个用户都有自己的个人资料。 Each user should be able to create profiles for their children, and have those profiles associated with their user account. 每个用户都应该能够为其子女创建个人资料,并将这些个人资料与其用户帐户相关联。 Children will not have their own user models. 儿童将没有自己的用户模型。 Therefore, each user could have multiple profiles. 因此,每个用户可以具有多个配置文件。 I would also like to do this through a relationship table. 我也想通过关系表来做到这一点。 Later down the road, if a user's spouse joins the system, I would like to be able to associate children to both parents. 后来,如果用户的配偶加入了系统,我希望能够将孩子与父母双方联系起来。

user.rb user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :id, :email, :password, :password_confirmation, :remember_me

  has_many :relationships

  has_many :profiles, :through => :relationships
  accepts_nested_attributes_for :profiles

  attr_accessible :profiles, :profiles_attributes
end

profile.rb profile.rb

class Profile < ActiveRecord::Base
  attr_accessible :first_name, :last_name

  has_many :relationships
  has_many :users, :through => :relationships
end

relationship.rb Relationship.rb

# Had to enter at least 6 characters to improve post (formatting was flawed)
class Relationship < ActiveRecord::Base
  attr_accessible :first_name, :last_name

  has_one :relationship_type

  belongs_to :user
  belongs_to :profile
end

new.html.erb new.html.erb

<h2>Sign Up for your account</h2>

<%= form_for(resource,  :as => resource_name, 
                        :url => registration_path(resource_name)) do |f| %>

  <%= devise_error_messages! %>

  <div>
    <%= f.label :email %><br />
    <%= f.email_field :email, :autofocus => true %>
  </div>

  <div>
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>

  <div>
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </div>

  <%= f.fields_for :profile do |builder| %>
    <div>
      <%= builder.label :first_name %><br />
      <%= builder.text_field :first_name %>
    </div>

    <div>
      <%= builder.label :last_name %><br />
      <%= builder.text_field :last_name %>
    </div>
  <% end %>

  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "devise/shared/links" %>

When browsing to the sign_up page, the form loads correctly. 浏览到sign_up页面时,该表单将正确加载。 On trying to save a new user, though.. I get the following error. 但是,在尝试保存新用户时。出现以下错误。

Can't mass-assign protected attributes: profile 无法批量分配受保护的属性:配置文件

In the new.html.erb, I have tried changing the line for the nested form to 在new.html.erb中,我尝试将嵌套表单的行更改为

<%= f.fields_for :profiles do |builder| %>

but that just caused the nested form to not get rendered. 但这只是导致嵌套表单无法呈现。

I would appreciate any help! 我将不胜感激任何帮助!

Thanks! 谢谢!

:profiles , not :profile :profiles ,不是:profile

<%= f.fields_for :profiles do |builder| %>
<div>
  <%= builder.label :first_name %><br />
  <%= builder.text_field :first_name %>
</div>

<div>
  <%= builder.label :last_name %><br />
  <%= builder.text_field :last_name %>
</div>

And you will need to build a profile in the controller: 您将需要在控制器中构建一个配置文件:

resource.profiles.build

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails,accepts_nested_attributes_for has_many:通过构建 - Rails, accepts_nested_attributes_for has_many: through with build Rails 6 使用 has_many 关系和 accepts_nested_attributes_for 连接表 - Rails 6 Joins Tables using has_many relationships and accepts_nested_attributes_for accepted_nested_attributes_for with has_many =&gt;:通过选项 - accepts_nested_attributes_for with has_many => :through Options has_many:through和accepts_nested_attributes_用于复制记录 - has_many :through and accepts_nested_attributes_for duplicating records Rails has_many / accepts_nested_attributes_for造成混乱吗? - Rails has_many/accepts_nested_attributes_for Create confusion? Rails accepts_nested_attributes_for通过以下方式不保存has_many上的关联 - Rails accepts_nested_attributes_for not saving association on has_many through Rails 4通过关系通过has_many接受hass_nested_attributes_for::_destroy无法正常工作 - Rails 4 accepts_nested_attributes_for for has_many through relationship: :_destroy is not working has_many accepts_nested_attributes_for关联问题 - has_many accepts_nested_attributes_for association question accepted_nested_attributes_for with has_many polymorphic - accepts_nested_attributes_for with has_many polymorphic accepts_nested_attributes_for has_many:通过创建和删除联接模型对象,具体取决于其他模型 - accepts_nested_attributes_for has_many :through Create and delete join model objects, depending on other model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM