我有一个错误的想法,但我无法修复它。嵌套资源3级深
为了再次解释我的情况,我有3个元素:Jobs,Questions and Answers。所有的关系都在下面设置。扩展我之前与Jobs> Questions关系有关的问题,我现在在Jobs> Questions> Answers中添加了Answers关系。
因此,在我的routes.rb中有一个新的资源,我收到了路由错误,我正在修复这些错误。这个问题发生时,我到了窗体的答案#新页面,并不得不修改form_for脚手架和创建行动在答案控制器(你可以看到我的代码为下面这两个)。
我是能够解决这些足以显示新的答案页面上的形式,但是当我点击提交我收到此错误:
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">}
从这个错误,我可以看到,我不节能job_id,我很确定这与我无关,而是在我的答案创建操作或答案新的form_for代码中正确地调用job_id。我尝试了很多解决方案,但似乎没有任何工作。我觉得我的创作行动很接近,但无法做到。无论如何,预先感谢任何帮助,如果我没有提供给您没有足够的周边代码,让我知道,我会添加它。
这个问题是这个职位的extenstion:Link_to Routing Issue With Nested Resources
附:我还添加了我的答案显示操作,因为如果我直接转到答案/ 1 /,它工作正常。我只是有一种感觉,如果我的创作行为是错误的,我的演出行动也是如此。
型号:
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
答案#新业态
<%= form_for(@answer, :url => job_question_answers_path(@answer.job_id, @question)) do |f| %>
答案创建行动
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
答案显示行动
def show
@job = Job.find(params[:job_id])
@question = @job.questions.find(params[:question_id])
@answer = @question.answers.find(params[:id])
end
查看此https://gist.github.com/1057810为我最新的耙路线。我知道我不应该嵌套超过1层,但它对我来说是最简单和最快捷的解决方案。
再次感谢!
嗨布雷特,感谢您的帮助和解释一切。不幸的是,我将这段代码添加到我的答案新动作中,并且仍然没有通过相同的错误正确保存job_id - “无路由匹配{:action =>”show“,:controller =>”answers“,:job_id => nil,:question_id => 1,:id =>#<答案id:1,job_id:nil,question_id:1,answer1:“dfgdg”,answer2:“dfgdg”,answer3:“dfgdg”,answer4:“dgd” ,answer5:“dgd”,created_at:“2011-07-05 22:43:15”,updated_at:“2011-07-05 22:43:15”>}'这是否意味着它必须是一个创建回答问题?这是服务器的一个pastie - http://www.pastie.org/2169613 – Igrabes