简体   繁体   English

accepts_nested_attributes_for不保存子表

[英]accepts_nested_attributes_for not saving children table

Connecting users and companies uses accepts_nested_attributes_for . 连接userscompanies使用accepts_nested_attributes_for When you add a user and save, the company information is not saved. 添加用户并保存时,不会保存公司信息。 Does anyone have any idea what could be the problem? 有谁知道可能是什么问题? Thanks in advance. 提前致谢。

user.rb user.rb

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

  attr_accessible :email, :password, :password_confirmation, :remember_me, :role_ids, :company_attributes

  has_one :company, :autosave => true

  accepts_nested_attributes_for :company

  has_and_belongs_to_many :roles


  def role?(role_name)
    return !!self.roles.find_by_name(role_name)
  end

  def with_company
    self.company.build
    self
  end

  private
    def create_role
      self.roles << Role.find_by_name(:user)  
    end
end

sign-up pages new.html.haml 注册页面new.html.haml

    %div{:style => "margin:10px"}
      %h2= t('devise.shared.links.sign_up')
      %br
      = form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :class => 'form-horizontal'}) do |f|
        = devise_error_messages!
        = f.fields_for :companies do |company_form|
          .control-group
            = company_form.label :name, :class => 'control-label'
            .controls
              = company_form.text_field :name
          .control-group
...
        .control-group
          = f.label :email, :class => 'control-label'
          .controls
            = f.text_field :email
        .control-group
          = f.label :password, :class => 'control-label'
          .controls
            = f.text_field :password
        .control-group
          = f.label :password_confirmation, :class => 'control-label'
          .controls
            = f.text_field :password_confirmation 
        .actions
          = f.submit t("Save"), :class => 'btn btn-primary'
          %br
          %br
          = render "links"

companies controller: https://gist.github.com/3863405 公司负责人: https//gist.github.com/3863405

$rails --version
Rails 3.2.2

$ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]

It looks like you are getting stopped up by mass-assignment protection, despite having attr_accessible :company_attributes in your model... 尽管您的模型中有attr_accessible :company_attributes ,但您似乎被批量分配保护所attr_accessible :company_attributes ...

Relevant log output: 相关日志输出:

WARNING: Can't mass-assign protected attributes: companies

Is that a direct copy/paste from your code, or is it possibly misspelled, or something, in the real code? 这是您代码中的直接复制/粘贴,还是在实际代码中可能是拼写错误或其他内容?

add :company_attributes to attr_accessible in ur user model 在您的用户模型中将:company_attributes添加到attr_accessible

add in User model 添加用户模型

def build_company(params = {})
  self.company = Company.new(params)
end

def company_attributes=(attributes)
  self.company = Company.new(attributes)
end

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM