Ryan Nielsen
2005-Aug-23 23:38 UTC
Using form helpers with a collection of new model objects
I''m working on my first Rails project and have hit a problem that I can''t seem to work around. Basically, I''m making an application that tracks access requests to various resources. When a user makes a request they should be presented with a set of questions which all should be answered. Those answers need to be stored along with all other request information. I''m including my models (stripped of validation code) as well as my request controller and a view that''s causing my problem. When the user selects a resource I grab all of the questions associated with that resource and make an array of Answer objects. My problem is this: I want to use form helpers in the view to present those objects and then to handle the display of errors and validation problems but the helper expects objects in a collection to have id''s which my new, unsaved objects don''t have. Is there some way to use form helpers with a collection of new objects that don''t have ids? It would be nice to leverage the form helpers and Active Record models to handle verification and error display and it seems like this would be a common activity... I''m at the point were I feel I''m missing something obvious. ;-) For reference, here are the models, controller and view in concern: app/model/resource.rb ---------------------------------- class Resource < ActiveRecord::Base has_and_belongs_to_many :questions has_many :requests end app/model/question.rb ---------------------------------- class Question < ActiveRecord::Base has_and_belongs_to_many :resources end app/model/answer.rb -------------------------------- class Answer < ActiveRecord::Base belongs_to :request end app/model/request.rb -------------------------------- class Request < ActiveRecord::Base belongs_to :resource has_many :answers end app/controllers/request_controller.rb ------------------------------------------------------- class RequestController < ApplicationController def index @resources = Resource.find(:all) end def make_request session[:resource] ||= Resource.find(@params[:resource][:id]) @answers = Array.new session[:resource].questions.each do |question| answer = Answer.new answer.question_text = question.text @answers << answer end end def complete_request request = Request.new request.resource = session[:resource] answers = Array.new @params[:answer].each do |answer| answer = Answer.new(answer.keys, answer.values) request.answers << answer end if request.save redirect_to_index(''Your request has been received. Expect a reply soon!'') else flash[:notice] = ''Unable to save request'' redirect_to(:action => ''make_request'' end end end app/viewes/request/make_request.rhtml -------------------------------------------------------------- <%= start_form_tag :action => ''complete_request'' %> <table> <% for @answer in @answers %> <tr> <td><%= answer.question_text %></td> <td><%= text_field ''answer[]'', ''text'' %></td> </tr> <% end %> </table> <%= submit_tag "Submit" %> <%= end_form_tag %> Thanks for any advice! -- Ryan
Ryan Nielsen
2005-Aug-26 00:48 UTC
Re: Using form helpers with a collection of new model objects
Let me try distilling this down to the key question: Is there a way to use Rails'' form helpers with a collection of new model objects? Since the new model objects haven''t been saved they don''t have associated ids that the form helpers seem to expect they have. I don''t want to save and then potentially later delete these objects, however, just to use the form helpers. Any suggestions? Am I going about my problem (described below...) in the completely wrong way? I welcome any tips and criticisms! -- Ryan On 8/23/05, Ryan Nielsen <rpnielsen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m working on my first Rails project and have hit a problem that I > can''t seem to work around. Basically, I''m making an application that > tracks access requests to various resources. When a user makes a > request they should be presented with a set of questions which all > should be answered. Those answers need to be stored along with all > other request information. I''m including my models (stripped of > validation code) as well as my request controller and a view that''s > causing my problem. > > When the user selects a resource I grab all of the questions > associated with that resource and make an array of Answer objects. My > problem is this: I want to use form helpers in the view to present > those objects and then to handle the display of errors and validation > problems but the helper expects objects in a collection to have id''s > which my new, unsaved objects don''t have. Is there some way to use > form helpers with a collection of new objects that don''t have ids? It > would be nice to leverage the form helpers and Active Record models to > handle verification and error display and it seems like this would be > a common activity... I''m at the point were I feel I''m missing > something obvious. ;-) > > For reference, here are the models, controller and view in concern: > > app/model/resource.rb > ---------------------------------- > class Resource < ActiveRecord::Base > has_and_belongs_to_many :questions > has_many :requests > end > > app/model/question.rb > ---------------------------------- > class Question < ActiveRecord::Base > has_and_belongs_to_many :resources > end > > app/model/answer.rb > -------------------------------- > class Answer < ActiveRecord::Base > belongs_to :request > end > > app/model/request.rb > -------------------------------- > class Request < ActiveRecord::Base > belongs_to :resource > has_many :answers > end > > app/controllers/request_controller.rb > ------------------------------------------------------- > class RequestController < ApplicationController > def index > @resources = Resource.find(:all) > end > > def make_request > session[:resource] ||= Resource.find(@params[:resource][:id]) > > @answers = Array.new > session[:resource].questions.each do |question| > answer = Answer.new > answer.question_text = question.text > @answers << answer > end > end > > def complete_request > request = Request.new > request.resource = session[:resource] > > answers = Array.new > @params[:answer].each do |answer| > answer = Answer.new(answer.keys, answer.values) > request.answers << answer > end > > if request.save > redirect_to_index(''Your request has been received. Expect > a reply soon!'') > else > flash[:notice] = ''Unable to save request'' > redirect_to(:action => ''make_request'' > end > end > end > > app/viewes/request/make_request.rhtml > -------------------------------------------------------------- > <%= start_form_tag :action => ''complete_request'' %> > <table> > <% for @answer in @answers %> > <tr> > <td><%= answer.question_text %></td> > <td><%= text_field ''answer[]'', ''text'' %></td> > </tr> > <% end %> > </table> > <%= submit_tag "Submit" %> > <%= end_form_tag %> > > > Thanks for any advice! > > -- > Ryan >