2013-01-05 3 views
1

동시에 두 개의 개체를 만들려고합니다. 내가 가진 최선의 방법은 fields_for를 사용하는 것입니다. 그 관계는 has_many입니다.레일 fields_for 내 필드를 볼 수 없습니다

<%= form_for(@event) do |f| %> 
    ... 
    <%= f.fields_for :locations do |e| %> 
    <%= e.text_field :longitude %> 
    <%= e.text_field :latitude %> 
    <% end %> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

내 필드는 표시되지 않습니다, I를 :

모델/location.rb

attr_accessible :address, :customer_id, :event_id, :latitude, :longitude 
    belongs_to :customer 
    belongs_to :event 

모델/event.rb

attr_accessible :locations_attributes 
    has_many :locations, :dependent => :destroy 
    accepts_nested_attributes_for :locations 

형태는 다음과 가지고있다 이 섹션의 문서 has_many를 따라 갔다 http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for 그리고 내 f.fiel에 ds_for : 위치가 필드가 표시되는 것보다 단수로 변경되지만 locations_attributes를 수정할 수 없기 때문에 위치를 만들 수 없습니다.

업데이트 :

내가 단수이면. 내 이벤트 모델로 변경

attr_accessible :description, :title, :location_attributes 

오류 내 컨트롤러가이

line 60: @event = Event.new(params[:event]) 
+0

당신이, 그것은 '해야 말했듯이 <% = f.fields_for : 위치가 할 | 전자 ​​| %>'not': locations' 왜 고칠 수 없습니까? – Khaled

+0

왜냐하면 내가 오류를 얻을 수 없다는 mass_assignment, 문서를 보면 그것도 멀티플을 가지고있다. – Jseb

+0

모델에 location_attributes를 추가한다. – msdq

답변

1

당신은 당신의 형태로이 일을해야처럼이

app/controllers/events_controller.rb:60:in `new' 
app/controllers/events_controller.rb:60:in `create' 

같다 : (위치하지 위치)

<%= f.fields_for :location do |e| %> 
    <%= e.text_field :longitude %> 
    <%= e.text_field :latitude %> 
<% end %> 

당신의 모델 event.rb에서

attr_accessible :description, :title, :locations_attributes (위치하지 위치)

관련 문제