为什么accepts_nested_attributes_for 不适合我? (导轨3)

发布于 2024-09-25 01:19:05 字数 2853 浏览 0 评论 0原文

我在 Rails 3 应用程序上使用 formattastic 和 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

相关控制器方法

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"

表单呈现正确,但是当我更改问题并单击保存时,记录不反映我的更改。我确实打开了 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"

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

I'm using formtastic and haml on a Rails 3 application. I'm trying to make a nested form for surveys and questions, but it's just not working for me. I've watched the railscast and on it and everything, but I can't seem to make it work for my application. So right now, I have the following:

models

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

relevant controller method

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

views

= 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"

The form renders correctly, but when I change a question and click save, the records doesn't reflect my changes. I do have debug(params) turned on, and here is what it comes back as:

--- !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"

What am I doing wrong here? I don't want to have to write these attribute changers manually!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2024-10-02 01:19:05

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

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

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

I am looking at some old code of mine that uses accepts_nested_attributes_for and I think what you need to do is change

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

to

attr_accessible :intro, :name, :pubdate, :enddate, :pubid, :questions_attributes
谢绝鈎搭 2024-10-02 01:19:05

我不允许 attr_accessible 中的某些内容。问题是,我不知道允许什么。我认为这是 questions_attributes,但它似乎不是这样工作的。

I was not allowing certain things in attr_accessible. Problem is, I don't know what to allow. I think it's questions_attributes, but it doesn't seem to work that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文