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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I think this is related to http://stackoverflow.com/questions/5914736/rails-3-undefined-method-model-name-for-arrayclass . I decided to change my ../ballot/new controller to: def new @survey = Survey.find(params[:survey_id]) @ballot = @survey.ballots.build # Prepare the ballot @ballot.preferences = @survey.questions.map{|question| question.preferences.build} #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 the new view to: <%= form_for [@survey, @ballot] do |f| %> <%=h @ballot.preferences[0].question.inspect %> <%= f.fields_for @ballot.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 %> And now I receive the error: undefined method `model_name'' for Array:Class for the fields_for line. My guess is @ballot.preferences isn''t a proper set of instances but just an array, though my Ruby on Rails skills aren''t good enough to figure this out. Any ideas? On Jul 10, 2011, at 11:14 PM, Christopher Thielen wrote:> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I was able to resolve this problem. For those curious: http://stackoverflow.com/questions/6656872/activerecord-building-for-nested-resource On Jul 11, 2011, at 11:09 AM, Christopher Thielen wrote:> I think this is related to http://stackoverflow.com/questions/5914736/rails-3-undefined-method-model-name-for-arrayclass . > > I decided to change my ../ballot/new controller to: > > def new > @survey = Survey.find(params[:survey_id]) > > @ballot = @survey.ballots.build > > # Prepare the ballot > @ballot.preferences = @survey.questions.map{|question| question.preferences.build} > #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 the new view to: > > <%= form_for [@survey, @ballot] do |f| %> > <%=h @ballot.preferences[0].question.inspect %> > > <%= f.fields_for @ballot.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 %> > > And now I receive the error: > undefined method `model_name'' for Array:Class > > for the fields_for line. > > My guess is @ballot.preferences isn''t a proper set of instances but just an array, though my Ruby on Rails skills aren''t good enough to figure this out. Any ideas? > > On Jul 10, 2011, at 11:14 PM, Christopher Thielen wrote: > >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Reasonably Related Threads
- dash symbol
- [RFC] Adding thread group semantics to LangRef (motivated by GPUs)
- [RFC] Adding thread group semantics to LangRef (motivated by GPUs)
- [RFC] Adding thread group semantics to LangRef (motivated by GPUs)
- [RFC] Adding thread group semantics to LangRef (motivated by GPUs)