rails : How can I call methods in the nested model from parents? -
this model.
survey
class survey < activerecord::base belongs_to :user has_many :questions, :dependent => :destroy accepts_nested_attributes_for :questions, :reject_if => lambda {|a| a[:content].blank?}, :allow_destroy => true
question : there is_correct(boolean) column indicates whether students right answer or not.
class question < activerecord::base belongs_to :survey has_many :answers, :dependent => :destroy accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
answer : there correct(boolean) column teacher checks making survey(test), , there user_answer(boolean) column students mark taking test.
class answer < actviverecord::base belongs_to :question
i want compare correct , user_answer in answer model, , save answer is_correct in question model.
there not basic crud methods generating question , answer, 3 methods answering(get)/grading(post) examination, showing(get) results. can check there no problem in routes result of rake routes.
updated question : changed controller , question model.
here methods in survey controller.
def answering @survey = survey.find(params[:id]) end def grading @survey = survey.find(params[:id]) @survey.user_id = current_user.id @survey.questions.each |q| q.auto_check end redirect_to results_survey_path(@survey) end def results end
here question controller.
def auto_check answers.each |a| is_correct = true if a.user_answer , a.correct self.save! end end
the result of rake routes.
$ rake routes | grep survey (in /home/seriousin/classcasts) answering_survey /surveys/:id/answering(.:format) surveys#answering grading_survey post /surveys/:id/grading(.:format) surveys#grading results_survey /surveys/:id/results(.:format) surveys#results surveys /surveys(.:format) surveys#index post /surveys(.:format) surveys#create new_survey /surveys/new(.:format) surveys#new edit_survey /surveys/:id/edit(.:format) surveys#edit survey /surveys/:id(.:format) surveys#show put /surveys/:id(.:format) surveys#update delete /surveys/:id(.:format) surveys#destroy
*the problem can't call survey object saved user input. *
i let results method empty. using redirect_to, don't need generate survey object.
def results #@survey = survey.where(params[:survey_id]) end
i think it's ok. because can pass survey object parameter grading method.
def grading @survey = survey.find(params[:id]) @survey.user_id = current_user.id @survey.questions.each |q| q.auto_check end redirect_to results_survey_path(@survey) end
but result 'undefined method `name' nil:nilclass'... how can use object contains user input?
thanks advanced.
your @survey.questions.each
gives instance of question model , calling method defined in questionscontroller
on it. had auto_check
been member of question
model wouldn't have received error.
if want keep auto_check
method in questionscontroller
think should pass in question
instance parameter follows:
def auto_check(question) question.answers.each |a| is_correct = true if a.user_answer , a.correct ... end end
and update call follows:
@survey.questions.each |q| auto_check(q) end
Comments
Post a Comment