2014-02-22 86 views
0

我正在嘗試重定向到答案路徑。無法重定向到路徑?

創建評論後,我有這樣的路徑

class CommentsController < ApplicationController 
    def create 
     @question = Question.find(params[:question_id]) 
     @answer = Answer.find(params[:answer_id]) 
     @comment = @answer.comments.new(params[:comment]) 
     @comment.writer = current_user.username 
     @comment.save 
      redirect_to question_answer_url 
    end 
end 

不過,我得到沒有路由匹配{:動作=> 「秀」,:控制器=> 「答案」}。不過,我肯定有我的答案控制器這種方法

def show 
    @question = Question.find(params[:question_id]) 
    @answer = Answer.find(params[:id]) 
    @comment = @answer.comments.new(params[:comment]) 
end 

我也嘗試redirect_to的question_answer_path([@問題,@answer]),但不工作或者作爲它說沒有路由匹配(我假設也許這是太多的信息?)。 >答案 - - >註釋

我不知道爲什麼我的重定向不工作

比如他們去如下問題我嵌套我的資源,我的路線。

回答

1

您需要重定向到的路徑,而不陣列括號:

redirect_to question_answer_path(@question,@answer) 
+0

真棒,謝謝!在控制器中重定向和在視圖中重定向有什麼區別?當我在視圖中使用上述格式時,我不會存在這樣的路由。我必須使用question_answer_path([@ question,@answer]),即使如此,我也得到了所有重定向的相同answer_id。 – user3340037

+0

在您重定向的控制器中,在您鏈接的視圖中。重定向發生在渲染視圖之前。視圖渲染完成後,您需要可點擊的對象。您可以輸入 <%= link_to「您的鏈接文字」,question_answer_path(@ question,@ answer)%> –