# Surveys

**URL:** <https://www.ruby-forum.com/t/surveys/226079>\
**Category:** Rails\
**Created:** [September 21, 2010, 6:08pm UTC](https://www.ruby-forum.com/t/surveys/226079 "2010-09-21T18:08:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [September 21, 2010, 6:08pm UTC](https://www.ruby-forum.com/t/surveys/226079/1 "2010-09-21T18:08:53Z")

</div>

Hello everyone -

I implemented a survey module based on Ryan B.'s Railscasts on nested  
forms:

[http://asciicasts.com/episodes/196-nested-model-form-part-1](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 %\>

## Survey for Request #: \<%= @request.id %\>

Please submit the following survey as it relates to your request for **\<%= @request.product.name %\>** , closed on: **\<%= @request.close\_date.strftime("%m/%d/%y") if @request.close\_date %\>**:

\<% @resultSet.each do |questionAnswerPair| %\> \<%= questionAnswerPair[0].content %\>  
 \<%= select("@response", "answer\_id", questionAnswerPair[1].collect {|answer| [answer.content, answer.id]}, :include\_blank =\> true) %\>

\<% end %\>

Additional Comments:  
\<%= f.text\_area :note, :size =\> "60x5" %\>

\<%= f.submit "Submit" %\>

\<% 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
