ruby on rails - Nesting Resources 3 Levels Deep -
i have idea of going wrong, having trouble fixing it.
to explain situation again, have 3 elements : jobs, questions , answers . of relationships set below. expanding on previous question had jobs > questions relationship, have added answers relationship in jobs > questions > answers.
so, new resource in routes.rb getting routing errors, fixing going. problem occurred when got form answers#new page , had modify form_for scaffolding , build on create action in answers controller ( can see code 2 below).
i able fix enough show form on new answers page, when click submit getting error:
no route matches {:action=>"show", :controller=>"answers", :job_id=>nil, :question_id=>1, :id=>#<answer id: 3, job_id: nil, question_id: 1, answer1: "", answer2: "", answer3: "", answer4: "", answer5: "", created_at: "2011-07-01 03:12:06", updated_at: "2011-07-01 03:12:06">}
from error can see not saving job_id , pretty sure has me not calling job_id in either answers create action or answers new form_for code. i've tried ton of solutions, nothing seems work. feel close on create action, can't right. anyways, in advance , if haven't provided without enough surrounding code, let me know , add it.
this question extenstion of post: link_to routing issue nested resources
p.s. added answers show action, because although working if go answers/1/ directly. have feeling if create action wrong, show action.
models:
class job < activerecord::base has_many :questions has_many :answers class question < activerecord::base belongs_to :job has_many :answers class answer < activerecord::base belongs_to :job belongs_to :question
answers#new form
<%= form_for(@answer, :url => job_question_answers_path(@answer.job_id, @question)) |f| %>
answers create action
def create @job = job.find(params[:job_id]) @question = @job.questions.find(params[:question_id]) @answer = @question.answers.build(params[:answer]) if @answer.save redirect_to(job_question_answer_path(@answer.job_id, @answer.question_id, @answer) end end
answers show action
def show @job = job.find(params[:job_id]) @question = @job.questions.find(params[:question_id]) @answer = @question.answers.find(params[:id]) end
see https://gist.github.com/1057810 current rake routes. know shouldn't nest more 1 layer deep, easiest , quickest solution me.
thanks again!
based on line:
no route matches { :action=>"show", :controller=>"answers", :job_id=>nil, #problem :question_id=>1, :id=>#<answer id: 3, job_id: nil, question_id: 1, answer1: "", answer2: "", answer3: "", answer4: "", answer5: "", created_at: "2011-07-01 03:12:06", updated_at: "2011-07-01 03:12:06">}
you can see job_id
in answer object nil. form helper uses job_question_answers_path
requires job_id
, question_id
, route error broken because job_id
nil (@answer.job_id
):
<%= form_for(@answer, :url => job_question_answers_path(@answer.job_id, @question)) |f| %>
try setting @answer.job
in answerscontroller#new
action explicitly.
def new # may have code this... @answer = answer.new # guessing on param names here: @answer.question = question.find params[:question_id] @answer.job = job.find params[:job_id] # alternatively, can set ids, # above verify objects exist first. @answer.job_id = params[:job_id] @answer.question_id = params[:question_id] end
Comments
Post a Comment