Ruby on Rails: Nested form not working
Currently, my nested form (the one for address) isn't showing up. It just shows the h1, and the submit button...
Relevant code:
class Address < ActiveRecord::Base
belongs_to :user
belongs_to :poll_option
apply_addresslogic :fields => [[:number, :street], :city, [:state, :zip_code]]
end
class PollOption < ActiveRecord::Base
belongs_to :poll
has_one :address
accepts_nested_attributes_for :address, :allow_destroy => true
end
<h1>Add a new address for voting</h1>
<% form_for @poll_option do |po_form| %>
<%= po_form.error_messages %>
<%= po_form.hidden_field :poll_id, :value => @poll.id %>
<% po_form.fields_for :address do |addr_form| %>
<%= addr_form.label :number %><br />
<%= addr_form.text_field :number %><br />
<br />
<%= addr_form.label :street %><br />
<%= addr_form.text_field :street %><br />
<br />
<%= addr_form.label :city %><br />
<%= addr_form.text_field :city %><br />
<br />
<%= addr_form.label :state %><br />
<%= addr_form.text_field :state %><br />
<br />
<%= addr_form开发者_Python百科.label :zip_code %><br />
<%= addr_form.text_field :zip_code %><br />
<br />
<br />
<% end %>
<%= submit_tag "Create address and vote for this one" %>
<% end %>
If you're using Rails 3, you should be using <%= form_for ..
and <%= f.fields_for ...
, as the =
on the tag indicates that it will output.
Additionally, in your controller's action that renders this form you need to be building the object (@poll_option.build_address
or similar) so that the fields_for
has an object to render.
精彩评论