Note that there are some explanatory texts on larger screens.

plurals
  1. POActiveRecord building for nested resource
    primarykey
    data
    text
    <p>(NOTE: Source code here <a href="https://github.com/cthielen/dss-evote" rel="nofollow">https://github.com/cthielen/dss-evote</a>)</p> <p>I've got a simple voting application. A survey is the set of questions to vote on, a ballot is a per-user instance of their preferences, and the ballot has_many preferences, which again, are unique to each user. Here's the modeling:</p> <pre><code>class Ballot &lt; ActiveRecord::Base belongs_to :survey has_many :preferences end class Survey &lt; ActiveRecord::Base has_many :questions has_many :eligibilities has_many :ballots accepts_nested_attributes_for :questions, :allow_destroy =&gt; true attr_accessible :title, :description, :status, :deadline, :questions_attributes def owner Person.find(owner_id) end end class Question &lt; ActiveRecord::Base belongs_to :survey has_many :preferences end class Preference &lt; ActiveRecord::Base belongs_to :ballot belongs_to :question end </code></pre> <p>routes.rb only has this: resources :surveys do resources :ballots end</p> <p>/surveys/1 seems to work, even /surveys/1/ballots. /surveys/1/ballots/new is where I run into issues:</p> <p>in ballots_controller.rb:</p> <pre><code>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 </code></pre> <p>(corresponding view)</p> <pre><code>&lt;%= form_for [@survey, @ballot] do |f| %&gt; &lt;%= f.fields_for @ballot.preferences do |preferences_fields| %&gt; &lt;% for question in @preferences_fields %&gt; &lt;p&gt; &lt;%= f.label question.question %&gt; &lt;%= radio_button(question.id, "preference", "Yes") %&gt; Yes &lt;%= radio_button(question.id, "preference", "No") %&gt; No &lt;%= radio_button(question.id, "preference", "Decline") %&gt; Decline &lt;/p&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;div class="actions"&gt; &lt;%= f.submit "Vote" %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Results in the error:</p> <pre><code>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: &lt;%= form_for [@survey, @ballot] do |f| %&gt; 2: &lt;% f.fields_for @ballot.preferences do |preferences_fields| %&gt; 3: &lt;% for question in @preferences_fields %&gt; 4: &lt;p&gt; 5: &lt;%= f.label question.question %&gt; </code></pre> <p>Now, it appears an array is being formed instead of proper instances of the class, but I'm at a loss for how to properly fix this.</p> <p>EDIT: I should mention the reason I'm attempting to build @ballot.preferences are that the preferences represent a person's answer, and the length of preferences may change from survey to survey. So if a survey has six questions, @ballot.survey.questions.length will be 6, and I need to create 6 blank @ballot.preferences, which will then be represented by form_for and hopefully saved properly using a RESTful Create.</p> <p>Thanks in advance for any help you can offer!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload