我已经查看了几十个StackOverflow基本上这个问题的答案,但没有一个很有帮助!首先,我使用的是Rails 3,所以强大的参数并不是答案。
我有SurveyUser模型和SurveyDemographic模型,每个SurveyUser都有一个SurveyDemographic,每个SurveyDemographic都属于SurveyUser。
class SurveyUser < ActiveRecord::Base
has_one :survey_demographic
accepts_nested_attributes_for :survey_demographic
attr_accessible :password, :username, :survey_demographic_attributes
end
class SurveyDemographic < ActiveRecord::Base
belongs_to :facility
belongs_to :survey_user
attr_accessible :age, :gender
end
我有一个嵌套表单来创建调查用户并填写他们的人口统计信息:
= form_for @survey_user, :url => facility_survey_users_path(@facility) do |f|
- if @survey_user.errors.any? || @survey_demographic.errors.any?
%div#error_explanation
%h2
= pluralize(@survey_user.errors.count + @survey_demographic.errors.count, "error") prohibited this survey_user from being saved:
%ul
- @survey_user.errors.full_messages.each do |msg|
%li= msg
- @survey_demographic.errors.full_messages.each do |msg|
%li= msg
%table.su
%tr
%td Username *
%td
= f.text_field :username
%tr
%td Password *
%td
= f.text_field :password
= fields_for :survey_demographics, @survey_user.survey_demographic do |a|
%tr
%td Gender
%td.gender
= a.text_field :gender
%tr
%td Age
%td.age
= a.text_field :age
%div#actions
= f.submit :class => 'button_link'
= link_to 'Cancel', facility_path(params[:facility_id]), :class => "button_link cancel_link"
表格显示,我是否使用fields_for或f.fields_for作为调查人口统计信息。当我查看日志时,我得到了正确的东西:
"survey_user"=>{"username"=>"Mona Lisa", "password"=>"[FILTERED]"},
"survey_demographics"=>{"gender"=>"1", "age"=>"99"}, "commit"=>"Create Survey user",
"facility_id"=>"21"}
但是,这个survey_demographics数据都没有保存到数据库中。我每次都使用survey_demographic_id创建survey_demographic记录,但所有条目都是零。我在日志中看到该条目已创建然后更新,但更新中不包含任何此类数据。
当我使用fields_for
时,我没有错误(它不起作用)。当我使用f.fields_for
时,我得到&#34;警告:无法为survey_demographics批量分配变量!&#34;即使我的SurveyUser模型中有attr_accessible :survey_demographic_attributes
。
我的survey_users_controller.rb
包含:
def new
@survey_user = SurveyUser.new
@survey_demographic = @survey_user.build_survey_demographic
@survey_user.facility = @facility
和
def create
@survey_user = SurveyUser.new(params[:survey_user])
@survey_user.facility = @facility
@survey_demographic = @survey_user.create_survey_demographic(params[:survey_demographic])
使用适当的结束语句,并且survey_demographic_controller没有任何内容不由Rails生成。
我应该使用fields_for
还是f.fields_for
?尽管我f.fields_for
,为什么attr_accessible
会导致质量分配错误?最终,为什么嵌套表单中的数据不能保存?
答案 0 :(得分:0)
创建嵌套表单并使用accepts_nested_attributes_for
时,Rails期望创建嵌套属性的键的格式为<association_name>_attributes
。在这种情况下,survey_demographic_attributes
,
您可以调整fields_for
以使用正确的密钥
= f.fields_for :survey_demographic_attributes, @survey_user.survey_demographic do |a|
但是,如果您只是这样做,Rails应该能够找到正确的密钥:
= f.fields_for @survey_user.survey_demographic do |a|
在您的代码示例中,您只使用fields_for
而不是f.fields_for
。使用f.fields_for
以便survery_demographic嵌套在survery_user下。
答案 1 :(得分:0)
最终有效:
= fields_for :survey_demographic do |a|
我不理解100%,但是:
现在哈希就是&#34;应该&#34;是:
参数:{&#34; utf8&#34; =&gt;&#34;✓&#34;,&#34; authenticity_token&#34; =&gt;&#34; Nc6bnb3USOtWyH8iC1 / 19AeEnvKhcVvPytJYNbFksuc =&#34; ,&#34; survey_user&#34; =&gt; {&#34;用户名&#34; =&gt;&#34; Real One&#34;,&#34;密码&#34; =&gt;&#34; [已过滤]&#34;},&#34; survey_demographic&#34; =&gt; {&#34;性别&#34; =&gt;&#34; 0&#34;,&#34;年龄&#34; =&gt; ;&#34; 88&#34;},&#34;提交&#34; =&gt;&#34;创建调查用户&#34;,&#34; facility_id&#34; =&gt;&#34; 21&# 34;}
特别是,这会传递survey_user => {stuff}, survey_demographic => {more stuff}
(不是真正的代码!)而不是survey_user => {stuff, survey_demographic => {more stuff}}
。请注意{}。
我还不明白为什么@ survey_user.survey_demographic是错误的。
感谢您的回答,伙计们 - 这有助于思考问题! Pavan的回答让我看到了钥匙,而Sharagoz正在考虑关键的名字和位置。