Skip to content

Instantly share code, notes, and snippets.

@salbito
Created October 22, 2010 00:32
Show Gist options
  • Save salbito/639680 to your computer and use it in GitHub Desktop.
Save salbito/639680 to your computer and use it in GitHub Desktop.
def new
@questions = Question.find_all_by_survey_id(params[:survey_id])
@answers = @questions.map{|q| q.answers.build}
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @answer }
end
end
Started GET "/surveys/1/answers/new" for 127.0.0.1 at 2010-10-21 20:24:59 -0400
Processing by AnswersController#new as HTML
Parameters: {"survey_id"=>"1"}
User Load (2.4ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 2) LIMIT 1
Question Load (0.4ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`survey_id` = 1)
Question Load (0.4ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`id` = 1) LIMIT 1
Question Load (0.2ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`id` = 3) LIMIT 1
Question Load (0.1ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`id` = 5) LIMIT 1
Question Load (0.2ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`id` = 7) LIMIT 1
Question Load (0.2ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`id` = 9) LIMIT 1
Rendered answers/_form.html.haml (28.1ms)
Rendered answers/new.html.haml within layouts/application (43.5ms)
Completed 200 OK in 182ms (Views: 50.7ms | ActiveRecord: 3.9ms)
New.html.haml
%h1 New answer
= render 'form'
_form.html.haml
= form_for 'answers[]', :url => survey_answers_path do |f|
- for answer in @answers
= fields_for 'answers[]', answer do |a|
%p= answer.question.question_string
= a.label :answer_string
= a.label answer.question_id
= a.text_field :answer_string
.actions
= f.submit
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :user
belongs_to :survey
attr_accessible :answer_string, :user_id, :question_id
end
class Question < ActiveRecord::Base
belongs_to :survey
attr_accessible :question_string, :question_type
validates_presence_of :question_string, :question_type
has_many :answers, :dependent => :destroy
end
class Survey < ActiveRecord::Base
has_many :questions, :dependent => :destroy
has_many :answers, :through => :questions
belongs_to :user
validates_presence_of :name, :user_id
accepts_nested_attributes_for :questions, :allow_destroy => true
accepts_nested_attributes_for :answers, :allow_destroy => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment