4

ネストされたフォームでRailsフォームを適切に作成する方法がわかりません。私は多くのチュートリアルに従いましたが、もっと混乱するようになると、それがどうあるべきか、単数の複数、コントローラー...ここに私のモデルがあります

model / event.rb

  attr_accessible :description :title, :tag_ids, :locations_attributes
  has_many :location
  accepts_nested_attributes_for :location, :allow_destroy => true

model / location.rb

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

controller.rb

  def new
    @event = Event.new
    ...
  def create
    @event = Event.new(params[:event])
    ...

form.html.erbを表示

<%= form_for(@event) do |f| %>
  <%= f.fields_for :locations do |e| %>
    <%= e.text_field :longitude %>
    <%= e.text_field :latitude %>
  <% end %>
  ...
<% end %>

エラー

Can't mass-assign protected attributes: locations

パラメータ送信

 "locations"=>{"longitude"=>"45.6666",
 "latitude"=>"47.44444665"},

fields_forがサポートしていないために関係が間違っているか、コントローラーが適切でないか、railsが優れた言語ではないか、理解できなくなっています。

4

2 に答える 2

2

あなたはもうすぐそこにいます...

event.rb-場所ではなく場所

attr_accessible :description :title, :tag_ids, :locations_attributes
has_many :locations
accepts_nested_attributes_for :locations, :allow_destroy => true

やるべきだと思う

編集

そしてValeryKvonが言うように、あなたは追加する必要があります

@event.locations.build

あなたのコントローラーに

于 2013-01-05T20:44:09.590 に答える
1

エドワードの答え+

def new
  @event = Event.new
  @event.locations.build
end
于 2013-01-05T21:46:05.207 に答える