Hello everyone -
I implemented a survey module based on Ryan Bates''s Railscasts on
nested
forms:
http://asciicasts.com/episodes/196-nested-model-form-part-1
I need users to take a survey upon close of a request they had
submitted. The survey builder works great, but Ryan didn''t include a
way to ANSWER the survey questions, so now I''m stuck.
I took a stab at it.........
added responses migration:
class CreateResponses < ActiveRecord::Migration
def self.up
create_table :responses do |t|
t.integer :request_id
t.integer :question_id
t.integer :answer_id
t.integer :survey_id
t.timestamps
end
end
def self.down
drop_table :responses
end
end
-----------------------------------------------------
created a new response model:
class Response < ActiveRecord::Base
belongs_to :survey
belongs_to :question
has_many :answers
end
------------------------------------------------------
added to the existing survey, question and answer models:
class Survey < ActiveRecord::Base
has_many :questions, :dependent => :destroy
has_many :responses
accepts_nested_attributes_for :questions
end
----------
class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers, :dependent => :destroy
has_many :responses
accepts_nested_attributes_for :answers
end
-----------
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :response
end
--------------------------------------------------------
created responses_controller with a survey_response method:
def survey_response
@request = Request.find(params[:id])
@survey = Survey.find(:first, :conditions => id @request.survey_id)
@question = Question.find(:all, :conditions => survey_id @survey.id)
@resultSet = Array.new
@question.each do |question|
answers = Answer.find(:all, :conditions => ["question_id =
?",
question.id])
questionAnswerPair = [question, answers]
@resultSet << questionAnswerPair
questionAnswerPair=[]
answers=[]
end
if request.post?
@response = Response.new(:request_id => @request.id,
:question_id => (params[:question_id],
:answer_id => (params[:answer_id]),
:survey_id => @survey.id,
:note => (params[:note])
)
if @response.save
flash[:notice] = "Your survey has been submitted."
redirect_to :controller => :requests, :action =>
''list_MyRequests''
@request.update_attribute(:survey_taken, true)
else
flash[:alert] = "Unable to submit your survey."
end
end
end
--------------------------------------------------------
created survey_response view:
<% form_for :response, @response, :url => {:controller => :responses,
:action => :survey_response, :id => params[:id]} do |f| %>
<%= f.error_messages %>
<h2>Survey for Request #: <%= @request.id %></h2>
<p>
Please submit the following survey as it relates to your request for
<b><%= @request.product.name %></b>, closed on:
<b><%@request.close_date.strftime("%m/%d/%y") if
@request.close_date %></b>:
<p>
<tr>
<% @resultSet.each do |questionAnswerPair| %>
<tr class="<%= cycle(''odd'',
''even'') %>">
<td><%= questionAnswerPair[0].content %></td><br>
<td><%= select("@response", "answer_id",
questionAnswerPair[1].collect
{|answer| [answer.content, answer.id]}, :include_blank => true)
%></td><p>
</tr>
<% end %>
</tr>
<p>
Additional Comments:<br>
<%= f.text_area :note, :size => "60x5" %>
<p>
<%= f.submit "Submit" %></p>
<% end %>
--------------------------------------------------------
The response view renders the question/answers ok, and upon submit, the
page redirects correctly. However, nothing is getting saved to the
database. Can anyone help me? I know I included alot of code, but I
want you to be able to see the whole picture... thanks so much for any
assistance you can provide!
-cb
--
Posted via http://www.ruby-forum.com/.
--
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.