Hello,

I think this is an easy one for the average Rails developer but I'm a bit stuck.

I'm creating a simple voting app: any user can create a survey, with any number 
of questions, and other users can then vote by creating a ballot for that 
survey.

Here's the modeling:

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 Ballot < ActiveRecord::Base
  belongs_to :survey
  has_many :preferences
end

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

To be clear: a survey is the set of questions and a ballot is a voter's set of 
answers (preferences).

I'm using the nested route: /surveys/[:survey_id]/ballot/new for the voting 
page.

What I'm having trouble with is pre-populating the ballot with empty 
preferences. When ballot/new is called, I do this:

def new
    @survey = Survey.find(params[:survey_id])
    
    @ballot = @survey.ballots.build
    
    # Prepare the ballot
    for question in @ballot.survey.questions
      p = Preference.new
      p.ballot = @ballot
      p.question = question
      @ballot.preferences << p
    end
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @ballot }
    end
  end

and on the ballot/new.html.erb page, I try this:

<%= form_for [@survey, @ballot] do |f| %>
  <%= f.fields_for :preferences do |preference_form| %>
    <%= preference_form.fields_for :question do |question_form| %>
      <p>
            <%= question_form.label :question %><br />
            <%= radio_button("preference", "preference", "Yes") %> Yes
            <%= radio_button("preference", "preference", "No") %> No
            <%= radio_button("preference", "preference", "Decline") %> Decline
      </p>
    <% end %>
  <% end %>

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

But this merely shows one set of radio buttons with no question text, though 
I've confirmed the @survey.questions are populated with questions.

Does anybody see what I'm doing wrong here? My guess is looking at the 
new.html.erb is going to really show off something incorrect.

Christopher Thielen

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to