Guest User

Untitled

a guest
Jun 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. def new
  2. @questions = Question.find_all_by_survey_id(params[:survey_id])
  3. @answers = @questions.map{|q| q.answers.build}
  4.  
  5. respond_to do |format|
  6. format.html # new.html.erb
  7. format.xml { render :xml => @answer }
  8. end
  9. end
  10.  
  11. Started GET "/surveys/1/answers/new" for 127.0.0.1 at 2010-10-21 20:24:59 -0400
  12. Processing by AnswersController#new as HTML
  13. Parameters: {"survey_id"=>"1"}
  14. User Load (2.4ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 2) LIMIT 1
  15. Question Load (0.4ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`survey_id` = 1)
  16. Question Load (0.4ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`id` = 1) LIMIT 1
  17. Question Load (0.2ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`id` = 3) LIMIT 1
  18. Question Load (0.1ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`id` = 5) LIMIT 1
  19. Question Load (0.2ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`id` = 7) LIMIT 1
  20. Question Load (0.2ms) SELECT `questions`.* FROM `questions` WHERE (`questions`.`id` = 9) LIMIT 1
  21. Rendered answers/_form.html.haml (28.1ms)
  22. Rendered answers/new.html.haml within layouts/application (43.5ms)
  23. Completed 200 OK in 182ms (Views: 50.7ms | ActiveRecord: 3.9ms)
  24.  
  25. New.html.haml
  26. %h1 New answer
  27. = render 'form'
  28.  
  29. _form.html.haml
  30. = form_for 'answers[]', :url => survey_answers_path do |f|
  31. - for answer in @answers
  32. = fields_for 'answers[]', answer do |a|
  33. %p= answer.question.question_string
  34. = a.label :answer_string
  35. = a.label answer.question_id
  36. = a.text_field :answer_string
  37. .actions
  38. = f.submit
  39.  
  40. class Answer < ActiveRecord::Base
  41. belongs_to :question
  42. belongs_to :user
  43. belongs_to :survey
  44. attr_accessible :answer_string, :user_id, :question_id
  45. end
  46.  
  47. class Question < ActiveRecord::Base
  48. belongs_to :survey
  49. attr_accessible :question_string, :question_type
  50. validates_presence_of :question_string, :question_type
  51. has_many :answers, :dependent => :destroy
  52. end
  53.  
  54. class Survey < ActiveRecord::Base
  55. has_many :questions, :dependent => :destroy
  56. has_many :answers, :through => :questions
  57. belongs_to :user
  58. validates_presence_of :name, :user_id
  59. accepts_nested_attributes_for :questions, :allow_destroy => true
  60. accepts_nested_attributes_for :answers, :allow_destroy => true
  61. end
Add Comment
Please, Sign In to add comment