accepts_nested_attributes_for: что я делаю не так
Я пытаюсь создать соединение «один ко многим» в rails4. Однако, хотя я не получаю сообщение об ошибке, вложенный атрибут не сохраняется.
Что я делаю неправильно?
Станция-Models
class Station < ActiveRecord::Base
has_many :adresses
accepts_nested_attributes_for :adresses
end
Адрес-модель
class Adress < ActiveRecord::Base
belongs_to :station
end
Станция-контроллер класс StationsController <ApplicationController
def new
@station = Station.new
@station.adresses.build
end
def create
@station = Station.new(station_params)
@station.save
redirect_to @station
end
def index
@stations = Station.all
end
private
def station_params
params.require(:station).permit(:name, adresses_attributes: [ :url ])
end
end
Станция: new.html.erb
<%= form_for :station, url: stations_path do |station| %>
<p>
<%= station.label :name %><br />
<%= station.text_field :name %>
</p>
<%= station.fields_for :adresses do |adress| %>
<div class="field">
<p>
<%= adress.label :url %><br />
<%= adress.text_field :url %>
</p>
</div>
<% end %>
<p>
<%= station.submit %>
</p>
<% end %>
[редактировать]
Я создал минимальный пример этой проблемы и задокументировал его как пошаговую инструкцию здесь:https://groups.google.com/forum/#!topic/rubyonrails-talk/4RF_CFChua0