2011-07-01 248 views
1

我有一個錯誤的想法,但我無法修復它。嵌套資源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層,但它對我來說是最簡單和最快捷的解決方案。

再次感謝!

回答

0

基於這一行:

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">} 

你可以看到在你的答案對象job_id爲零。你的表單輔助使用job_question_answers_path這需要一個job_idquestion_id,但路線錯誤被打破,因爲job_id爲零(@answer.job_id):

<%= form_for(@answer, :url => job_question_answers_path(@answer.job_id, @question)) do |f| %> 

嘗試在你的AnswersController#new動作設置@answer.job明確。

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 
+0

嗨佈雷特,感謝您的幫助和解釋一切。不幸的是,我將這段代碼添加到我的答案新動作中,並且仍然沒有通過相同的錯誤正確保存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