2012-10-10 6 views
1

userscompanies을 연결하면 을 사용합니다. 사용자를 추가하고 저장하면 회사 정보가 저장되지 않습니다. 누구든지 문제가 될 수있는 아이디어가 있습니까? 미리 감사드립니다.accepts_nested_attributes_for 아이들 테이블을 저장하지 않음

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 

가입 페이지 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" 

회사 컨트롤러 : 당신처럼 https://gist.github.com/3863405

$rails --version 
Rails 3.2.2 

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

당신의 PARAMS 검사의 출력을 게시 할 수있는 사용자 모델에 추가? –

+0

출력 : https://gist.github.com/3863459 – Nar

답변

1

같습니다 게티

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

이 코드에서 직접 복사/붙여 넣기, 또는 아마도 철자가 잘못 즉, : NG는

관련 로그 출력 ... 모델의 attr_accessible :company_attributes을에도 불구하고, 대량 할당 보호에 의해 중단 또는 실제 코드에서 무엇인가?

+0

또는 att_accessible 일 필요가 있습니다. companies_attributes ... –

+0

회사 주소가 변경되었지만 작동하지 않습니다. – Nar

+0

fields_for 입력 : 회사 –

0

추가 company_attributes 통해 UR 사용자 모델에 attr_accessible하는

들이 컨트롤러에 와서

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

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

: company_attributes가 이미 추가되었습니다. companies_attributes로 변경되었지만 작동하지 않습니다. – Nar

관련 문제