2016-08-15 6 views
3

평소와 같이 #new -action의 양식을 사용하여 사람에게 주소를 추가하려고 할 때 문제가 발생합니다. af.text_field를 사용하여, 다음 f.fields_for :address do |af| 및 필드를 다음 양식, 그리고 - 나는 현재하고 있어요 무엇
은 내 양식에서 다형성 1 대 1로 중첩 된 매개 변수

class Person < ActiveRecord::Base 
    has_one :address, :as => :owner 

    accepts_nested_attributes_for :address 
end 

class Address < ActiveRecord::Base 
    belongs_to :owner, :polymorphic => true 
end 

, 나는 form_for @person do |f|을 사용하고 있습니다.

내 컨트롤러 액션 :

def create 
    person = Person.new(person_params) 

    if person.valid? 
    person.save 
    redirect_to edit_content_person_path(@current_work_context, person) 
    else 
    @person = person 
    render 'new' 
    end 
end 

def person_params 
    params.require(:person).permit(:name, :address => [:line_one, :line_two, :zipcode, :city]) 
end 

나는 이러한 매개 변수와 함께, 내 양식을 제출 ActiveRecord::AssociationTypeMismatch

Address(#70198415678880) expected, got ActionController::Parameters(#70198414055220) 

내가없는 것 :

{"utf8"=>"✓", 
"authenticity_token"=>"blablabla==", 
"person"=>{"name"=>"mr man", 
"address"=>{"line_one"=>"test address 15", 
"line_two"=>"", 
"zipcode"=>"2600", 
"city"=>"Glostrup"}}, 
"locale"=>"da", 
"context_id"=>"9"} 

을하지만, 나는 예외를 얻을 이유를 알아 내야합니다.

편집 - 내보기, 단순화는 :

<%= form_for @person do |f| %> 

    <%= f.text_field :name %> 

    <%= f.fields_for :address do |af| %> 
    <%= af.label :address %> 
    <%= af.text_field :line_one, :placeholder => "Address 1" %> 
    <%= af.text_field :line_two, :placeholder => "Address 2" %> 

    <%= af.label :zipcode %> 
    <%= af.text_field :zipcode, :class => "form-control" %> 

    <%= af.label :city %> 
    <%= af.text_field :city, :class => "form-control" %> 

    <% end %> 

<% end %> 
+0

서버 바랍니다이 라인과 다시 시작 당신이 오류가 무엇을 줄 알고 도움이 될 것입니다. –

+0

오, 그래 - 맞아. 'Person.new' 라인에 –

+0

매우 이상합니다. 'accept._nested_attributes_for'를'f.fields_for : address'로 설정하면 params가 다음과 같이 변형됩니다. "person"=> { "name"=> "mr man", "address_attributes"=> { "line_one"=> "test address 15"' –

답변

1

하자 한 가지를 할려고.

# config/initializers/inflections.rb 

ActiveSupport::Inflector.inflections do |inflect|' 
    inflect.irregular 'address', 'addresses' 
end 

편집

# after we discussing more about the problem in chat we found the solution. 
# The object 'Person' not initialize correctly to f.fields_for :address 

# view 

<% f.object.build_address if f.object.address.blank? %> #add this line 
<%= f.fields_for :address do |af| %> 
+0

성공하지 못했다. 슬프게도. 나는 같은 예외를 얻는다. –

+0

구현의 차이점은 나의 것보다'addresses.rb' 클래스의 이름이고 has_many는 다음과 같습니다. has_many : addresses, class_name : 'Addresses', : : addressable' –

+0

나는'address.rb'와 일대일 관계 .. –