2011-09-08 2 views
1

중첩 된 양식에서 주어진 속성을 가져올 수 없습니다. 내 정확한 구성으로이 문제와 관련된 다른 게시물을 찾을 수 없었습니다.accepts_nested_attributes_for/fields_for 아무것도하지 않음

다음은 약식 모델입니다.

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

    attr_accessible :email, :password, :password_confirmation, :remember_me 
    attr_accessible :first_name, :last_name, :nickname 
    attr_accessible :current_organization_attributes 

    has_many :created_organizations, :class_name => "Organization", :foreign_key => :creator_user_id, :inverse_of => :creator_user 
    belongs_to :current_organization, :class_name => "Organization", :foreign_key => :current_org_id # one-way relationship 

    accepts_nested_attributes_for :current_organization 
    ... 
end 

class Organization < ActiveRecord::Base 
    belongs_to :creator_user, :class_name => "User", :foreign_key => "creator_user_id", :inverse_of => :created_organizations 
    ... 
end 

그리고 약식 스키마 :

#main 
    %h1 Your account 
    = form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :autocomplete => "off" }) do |f| 
    = devise_error_messages! 
    .edit-account-hold 
     %span About you 
     = f.label :first_name, "First Name" 
     = f.text_field :first_name, :required => @contact_request_is_pending 
     ... 

    .edit-account-hold 
     %span Your business 
     = f.fields_for :current_organization do |o| 
     = o.label :zip_code, "Zip Code" 
     = o.text_field :zip_code, :required => @contact_request_is_pending 
     = o.label :phone_number, "Phone Number" 
     = o.text_field :phone_number, :required => @contact_request_is_pending 

    .edit-account-hold 
     %span Your password 
     = f.label :password 
     = f.password_field :password 
     = f.label :password_confirmation 
     = f.password_field :password_confirmation 
     = f.label :current_password 
     = f.password_field :current_password 

     = f.submit "Update", :class => "orange-button border-radius" 
:

여기
# == Schema Information 
# 
# Table name: users 
# 
# id     :integer(4)  not null, primary key 
# email    :string(255)  default(""), not null 
# current_org_id  :integer(4) 
# first_name   :string(255) 
# last_name   :string(255) 
# nickname    :string(255) 
# 

# == Schema Information 
# 
# Table name: organizations 
# 
# id      :integer(4)  not null, primary key 
# name     :string(100) 
# creator_user_id   :integer(4)  not null 
# zip_code    :string(255) 
# phone_number   :string(255) 
# 

이 단축 뷰 코드 (HAML) 당신이 볼 수 있듯이, 사용자 및 조직 사이의 연관성은 조금 복잡하다

컨트롤러 코드 :

def update 
    if resource.update_with_or_without_password_as_needed(params[resource_name]) 
    set_flash_message :notice, :updated 
    redirect_to after_update_path_for(resource) 
    else 
    clean_up_passwords(resource) 
    render_with_scope :edit 
    end 
end 
{"utf8"=>"✓", "authenticity_token"=>"8aL7bMJdVI2uaLt3WoZEraSB0U5iZgBvxYh5fwsQnqM=", "user"=>{"first_name"=>"Nick", "last_name"=>"", "nickname"=>"", "email"=>"[email protected]", "current_organization_attributes"=>{"zip_code"=>"12345", "phone_number"=>"1112223333", "id"=>"1000003"}, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Update"} 

중첩 된 해시 PARAMS [ "사용자"] [ "current_organization_attributes은"] 저장에 실패 특성을 포함 414,는 여기에 "업데이트"를 클릭하면 전달되는 PARAMS의 샘플입니다. 서버 내 디버거를 사용하여, 나는 적절한시기에

user.current_organization.update_attributes(the_above_mentioned_current_organization_attributes_hash) 

에 대한 호출이 위대한 작품을 확인하고 내가 좋아하는 것처럼 속성을 업데이트했다. 시스템이 자동으로 이렇게하지 않는 이유는 무엇입니까? 그것은 대체 클래스 이름이나 외래 키를 사용하여 belongs_to 관계와 관련이 있습니까?

도움말!

답변

0

IIRC, accepts_nested_attributes_forhas_one 또는 has_many 연관체에 대해서만 작동합니다 ... 적어도 문서의 굵은 글씨가 드러납니다. 나는 그것이 belongs_to에서 작동한다고 생각하지 않습니다.

내 최고의 제안은 사용자 모델의 연결을 변경하는 것입니다 (이뿐만 아니라 스키마 변경이 필요) 같은 것을 할 다음 organizationscurrent라는 열이

has_many :organizations 
has_one :current_organization, :class_name => "Organization", :conditions => {:current => true} 

을하고 있음을 확인 하나의 조직 만이 현재의 상태 일 수 있습니다. 콜백을 사용하면 해당 사용자의 조직에 대한 다른 모든 current 값이 false으로 변경됩니다 (해당 사용자 범위로 분명히 표시됨).

fields_foraccepts_nested_attributes_for은 의도 한대로 작동해야합니다.

+0

흠. 이 함수를 작동 시키려고 노력하면서, belongs_to 연관을 위해 accepts_nested_attributes_for를 사용하는 사람들의 많은 예를 보았습니다. 그리고 대부분은 그것을 작동시킬 수있는 것처럼 보였습니다. 이 문제에 대해 다시 살펴 보 겠지만 이전에 두 지침이 호환 가능하다고 믿게되었습니다. –

관련 문제