【问题标题】:ActiveRecord building for nested resource嵌套资源的 ActiveRecord 构建
【发布时间】:2011-10-03 04:00:55
【问题描述】:

(注意:此处为源代码https://github.com/cthielen/dss-evote

我有一个简单的投票应用程序。调查是要投票的一组问题,选票是每个用户的偏好实例,而选票 has_many 偏好对于每个用户都是唯一的。这是建模:

class Ballot < ActiveRecord::Base
  belongs_to :survey
  has_many :preferences
end

class Survey < ActiveRecord::Base
  has_many :questions
  has_many :eligibilities
  has_many :ballots

  accepts_nested_attributes_for :questions, :allow_destroy => true

  attr_accessible :title, :description, :status, :deadline, :questions_attributes

  def owner
    Person.find(owner_id)
  end
end

class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :preferences
end

class Preference < ActiveRecord::Base
  belongs_to :ballot
  belongs_to :question
end

routes.rb 只有这个: 资源:调查做 资源:选票 结束

/surveys/1 似乎有效,甚至 /surveys/1/ballots。 /surveys/1/ballots/new 是我遇到问题的地方:

在 ballots_controller.rb 中:

def new
  @survey = Survey.find(params[:survey_id])

  @ballot = @survey.ballots.build

  @survey.questions.count.times { @ballot.preferences.build }

  respond_to do |format|
    format.html # new.html.erb
  end
end

(对应视图)

<%= form_for [@survey, @ballot] do |f| %>
  <%= f.fields_for @ballot.preferences do |preferences_fields| %>
    <% for question in @preferences_fields %>
      <p>
    <%= f.label question.question %>
    <%= radio_button(question.id, "preference", "Yes") %> Yes
    <%= radio_button(question.id, "preference", "No") %> No
    <%= radio_button(question.id, "preference", "Decline") %> Decline
  </p>
    <% end %>
  <% end %>

  <div class="actions">
    <%= f.submit "Vote" %>
  </div>
<% end %>

导致错误:

NoMethodError in Ballots#new

Showing /Users/cthielen/Projects/Work/dss-evote/app/views/ballots/_form.html.erb where line #2 raised:

undefined method `model_name' for Array:Class
Extracted source (around line #2):

1: <%= form_for [@survey, @ballot] do |f| %>
2:   <% f.fields_for @ballot.preferences do |preferences_fields| %>
3:     <% for question in @preferences_fields %>
4:       <p>
5:      <%= f.label question.question %>

现在,似乎正在形成一个数组而不是类的正确实例,但我不知道如何正确解决这个问题。

编辑:我应该提到我尝试构建@ballot.preferences 的原因是偏好代表一个人的答案,并且偏好的长度可能会因调查而异。因此,如果调查有六个问题,@ballot.survey.questions.length 将是 6,我需要创建 6 个空白 @ballot.preferences,然后将由 form_for 表示,并希望使用 RESTful Create 正确保存。

提前感谢您提供的任何帮助!

【问题讨论】:

  • 请正确提出您的问题...谢谢:)

标签: ruby-on-rails activerecord nested-resources


【解决方案1】:

好的,这是由于这一行:

@ballot.preferences = @survey.questions.map{|question| question.preferences.build}

因为映射创建了一个不能被form_helper 使用的数组(期望模型名称通常由 ActiveRecord 关系提供)。

你应该坚持这样的:

@survey.questions.count.times { @ballot.preferences.build }

旁注:

<% fields_for @ballot.preferences do |preferences_fields| %>

应该是:

<%= f.fields_for :preferences do |preferences_field| %>

【讨论】:

  • 对不起格式,我想我已经纠正了大部分。
  • 所以我添加了一条说明我为什么要尝试构建@ballot.preferences - 基本上,如果调查有六个问题,我需要提前创建六个空的@ballot.preferences。这有意义吗?
  • 我再次编辑了问题,以反映您对新方法和 HTML 表单本身提出的更改。不幸的是,我仍然遇到大致相同的错误:未定义的方法 `model_name' for Array:Class 。
  • 我在github.com/cthielen/dss-evote 更新了我的代码,以防更容易分析。
  • (你仍然缺少&lt;% f.fields_for中的=
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多