I have an idea of what is going wrong, but am having trouble fixing it.
To explain my situation again, I have 3 elements : Jobs, Questions and Answers . All of the relationships are set up below. Expanding on my previous question which had to do with Jobs >开发者_如何学运维 Questions relationship, I have now added the Answers relationship in Jobs > Questions > Answers.
So, with a new resource in my routes.rb I was getting routing errors, which I was fixing as I was going. The problem occurred when I got to the form Answers#new page and had to modify the form_for scaffolding and build on the Create action in the Answers controller ( you can see my code for those two below).
I was able to fix those enough to show the form on the new answers page, but when I click submit I am getting this 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 this error I can see that I am not saving the job_id and I am pretty sure this has to do with me not calling the job_id properly in either my answers create action or answers new form_for code. I've tried a ton of solutions, but nothing seems to work. I feel like I am close on my create action, but can't get it right. Anyways, thanks in advance for any help and if I haven't provided you without enough surrounding code, let me know and I will add it.
This question is an extenstion of this post: Link_to Routing Issue With Nested Resources
P.S. I also added my answers show action, because although it is working properly if I go to the answers/1/ directly. I just have a feeling that if my create action is wrong, so is my 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)) do |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 this https://gist.github.com/1057810 for my most current rake routes. I know I shouldn't nest more than 1 layer deep, but it was the easiest and quickest solution for me.
Thanks again!
Based on this 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 that the job_id
in your Answer object is nil. Your form helper uses job_question_answers_path
which requires a job_id
and a question_id
, but the route error is broken because the job_id
is nil (@answer.job_id
):
<%= form_for(@answer, :url => job_question_answers_path(@answer.job_id, @question)) do |f| %>
Try setting @answer.job
in your AnswersController#new
action explicitly.
def new
# You may already have code that does this...
@answer = Answer.new
# Guessing on param names here:
@answer.question = Question.find params[:question_id]
@answer.job = Job.find params[:job_id]
# Alternatively, you can probably just set the ids,
# but the above will verify those objects exist first.
@answer.job_id = params[:job_id]
@answer.question_id = params[:question_id]
end
精彩评论