gpt4 book ai didi

ruby-on-rails - 为什么 accepts_nested_attributes_for 对我不起作用? ( rails 3)

转载 作者:行者123 更新时间:2023-12-01 06:11:24 25 4
gpt4 key购买 nike

我在 Rails 3 应用程序上使用 formtastic 和 haml。我正在尝试为调查和问题制作一个嵌套表格,但它对我不起作用。我看过 railscast 和它上面的一切,但我似乎无法让它适用于我的应用程序。所以现在,我有以下内容:

模型

class Survey < ActiveRecord::Base
attr_accessible :intro, :name, :pubdate, :enddate, :pubid

belongs_to :user
has_many :questions, :dependent => :destroy, :autosave => true

accepts_nested_attributes_for :questions, :allow_destroy => true
end

class Question < ActiveRecord::Base

belongs_to :survey
has_many :answers, :dependent => :destroy

attr_accessible :q_text, :order, :q_type

end

相关 Controller 方法

def update
@survey = Survey.find(params[:id])
@user = current_user
if check_auth_and_redirect @user, @survey
if @survey.update_attributes(params[:survey])
flash[:success] = "Survey Updated"
redirect_to edit_survey_path(@survey)
else
@title = "Editing Survey #{@survey.id}"
render 'edit'
end
end
end

观看次数

= semantic_form_for @survey do |f|
= render "shared/survey_inputs", :object => f
= f.inputs :for => :questions, :name => "Survey Questions" do |fq|
%hr
= fq.input :q_text, :label => "Question text"
= fq.input :q_type,
:label => "Question type",
:as => :select,
:collection => %w(text scale radio select)
= fq.input :order, :label => "Question order"

表单正确呈现,但是当我更改问题并单击“保存”时,记录不会反射(reflect)我的更改。我确实打开了 debug(params),这是它返回的内容:

--- !map:ActiveSupport::HashWithIndifferentAccess 
utf8: "\xE2\x9C\x93"
_method: put
authenticity_token: CJCc9LvdoPjxwGJkhUnZjR0Z/c5Wt5VBT3bBr/wB4+A=
survey: !map:ActiveSupport::HashWithIndifferentAccess
name: This is my survey
intro: I've got a lovely bunch of coconuts. There they are all standing in a row.
pubid: smargdab
pubdate(1i): ""
pubdate(2i): ""
pubdate(3i): ""
enddate(1i): ""
enddate(2i): ""
enddate(3i): ""
questions_attributes: !map:ActiveSupport::HashWithIndifferentAccess
"0": !map:ActiveSupport::HashWithIndifferentAccess
q_text: one one one one one one one one one one one
q_type: text
order: "3"
id: "1"
"1": !map:ActiveSupport::HashWithIndifferentAccess
q_text: 2 2 2 2 2 2 2 2 2 2
q_type: text
order: "1"
id: "2"
"2": !map:ActiveSupport::HashWithIndifferentAccess
q_text: 3 3 3 3 3 3 3 3 3 3
q_type: text
order: "2"
id: "3"
commit: Update Survey
action: update
controller: surveys
id: "2"

我在这里做错了什么?我不想手动编写这些属性转换器!

最佳答案

我正在查看我的一些使用 accepts_nested_attributes_for 的旧代码,我认为您需要做的是更改

attr_accessible :intro, :name, :pubdate, :enddate, :pubid

attr_accessible :intro, :name, :pubdate, :enddate, :pubid, :questions_attributes

关于ruby-on-rails - 为什么 accepts_nested_attributes_for 对我不起作用? ( rails 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3765810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com