중첩 된 양식에서 주어진 속성을 가져올 수 없습니다. 내 정확한 구성으로이 문제와 관련된 다른 게시물을 찾을 수 없었습니다.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 관계와 관련이 있습니까?
도움말!
흠. 이 함수를 작동 시키려고 노력하면서, belongs_to 연관을 위해 accepts_nested_attributes_for를 사용하는 사람들의 많은 예를 보았습니다. 그리고 대부분은 그것을 작동시킬 수있는 것처럼 보였습니다. 이 문제에 대해 다시 살펴 보 겠지만 이전에 두 지침이 호환 가능하다고 믿게되었습니다. –