Reputation: 569
I have a simple survey with several parts which are themselves models. I'm adding the parts to the survey form one at a time, but the first one's failing. The problem is, when the form is rendered, the "demographics_part" field is not not showing up in the form.
My models all tie together correctly, as proven in the console:
Loading development environment (Rails 4.2.0)
2.2.0 :001 > s = Survey.last
Survey Load (0.6ms) SELECT `surveys`.* FROM `surveys` ORDER BY `surveys`.`id` DESC LIMIT 1
=> #<Survey id: 1, confirmed_at: nil, created_at: "2015-01-20 20:06:38", updated_at: "2015-01-20 20:06:38">
2.2.0 :002 > s.demographics_part
DemographicsPart Load (0.6ms) SELECT `demographics_parts`.* FROM `demographics_parts` WHERE `demographics_parts`.`survey_id` = 1 LIMIT 1
=> #<DemographicsPart id: 3, name: "foo", created_at: "2015-01-20 20:06:38", updated_at: "2015-01-20 20:06:38", survey_id: 1>
2.2.0 :003 > d = DemographicsPart.find(3)
DemographicsPart Load (0.9ms) SELECT `demographics_parts`.* FROM `demographics_parts` WHERE `demographics_parts`.`id` = 3 LIMIT 1
=> #<DemographicsPart id: 3, name: "foo", created_at: "2015-01-20 20:06:38", updated_at: "2015-01-20 20:06:38", survey_id: 1>
2.2.0 :004 > d.survey
Survey Load (0.8ms) SELECT `surveys`.* FROM `surveys` WHERE `surveys`.`id` = 1 LIMIT 1
=> #<Survey id: 1, confirmed_at: nil, created_at: "2015-01-20 20:06:38", updated_at: "2015-01-20 20:06:38">
2.2.0 :005 > s = Survey.new
=> #<Survey id: nil, confirmed_at: nil, created_at: nil, updated_at: nil>
2.2.0 :006 > s.build_demographics_part
=> #<DemographicsPart id: nil, name: nil, created_at: nil, updated_at: nil, survey_id: nil>
2.2.0 :007 > d = s.build_demographics_part
(0.2ms) BEGIN
(0.1ms) COMMIT
=> #<DemographicsPart id: nil, name: nil, created_at: nil, updated_at: nil, survey_id: nil>
2.2.0 :008 > d.name = "bar"
=> "bar"
2.2.0 :009 > s.save
(0.3ms) BEGIN
SQL (0.5ms) INSERT INTO `surveys` (`created_at`, `updated_at`) VALUES ('2015-01-20 21:09:02.625560', '2015-01-20 21:09:02.625560')
SQL (0.4ms) INSERT INTO `demographics_parts` (`name`, `survey_id`, `created_at`, `updated_at`) VALUES ('bar', 2, '2015-01-20 21:09:02.630175', '2015-01-20 21:09:02.630175')
(9.6ms) COMMIT
=> true
Here's my form:
<%= form_for(@survey) do |f| %>
<div class="row collapse">
<div class="small-3 columns">
<%= f.label :confirmed_at, class: "right inline", title: "Last Confirmed", data: {tooltip: true} %>
</div>
<div class="small-9 columns"><%= @survey.confirmed_at %></div>
</div>
<% f.fields_for :demographics_part do |d| %>
<%= d.label :name, "Name" %>
<% end %>
<div class="row collapse">
<div class="small-9 small-offset-3 columns"><%= f.submit %></div>
</div>
<% end %>
Here's my controller:
# GET /surveys/new
def new
@survey = Survey.new
@d = @survey.create_demographics_part
end
I don't see what's wrong.
Upvotes: 0
Views: 130
Reputation: 7779
Change
<% f.fields_for :demographics_part do |d| %>
with:
<%= f.fields_for :demographics_part do |d| %>
The error is because you are not printing the fields_for (the =
after %
)
Upvotes: 2