0

act_as_votable ジェムを使用して、質問に対する賛成票を獲得しようとしています。次のようなエラーが表示されます。

Couldn't find Question with id=like

この URL には質問 ID が表示されません:

/comments/1/questions//like

質問IDを手動で入力すると、次のようになります。

No route matches [GET]

これが私の賛成方法です:

def upvote
@question = Question.find params[:question_id]
@question.liked_by current_user
redirect_to @questions
end

これが routes.rb ファイルです。

resources :comments do
  resources :questions do
    put :upvote, :on => :member, :as => :like
  end
end

および賛成ボタン:

<%= link_to "Upvote", comment_question_like_path(@comment, @question), method: :post %>

Rake ルートは comment_question_like_path を有効なルートとして表示するため、問題はありません。助けてくれてありがとう!

4

3 に答える 3

0

次のコードで試してください。

あなたのルートで

resources :comments do
  resources :questions do
   put :upvote, :on => :member, :as => :like
  end
end

あなたの見解では

<%= link_to "Upvote", like_comment_question_path(@comment, @question), method: :put %>

コントローラーで

def upvote
  @question = Question.where('id = ? and comment_id = ?', params[:id], params[:comment_id]).first
  unless @question.blank? 
    @question.liked_by current_user
    redirect_to comment_question_path(params[:comment_id], @question)
  else
    flash[:error] = 'Question not found'
    redirect_to comment_questions_path(params[:comment_id])
  end
end
于 2013-09-14T04:59:20.463 に答える
0

あなたはページ/comments/2/questionsにいると言っています。つまりquestions#index、現在のコメントのすべての質問をロードし、すべての質問をループして@questions.each do |question|、各質問のリンクは次のようになります。

<%= link_to "Upvote", comment_question_like_path(@comment, question) %>

ではない:

<%= link_to "Upvote", comment_question_like_path(@comment, @question) %>

@question利用可能な変数がないため/comments/1/questions//like、質問のパラメーターが設定されていないため、次のようにする必要があります /comments/1/questions/5/like

編集


私のアプリで私のために働いた方法、ルート:

resources :comments do
  resources :questions do
    member do
      get :upvote
    end
  end
end

リンク:

<%= link_to "Upvote", comment_question_upvote_path(@comment, question) %>

method: postリンクに指定する必要があるルートを構築した方法です。

于 2013-09-27T07:09:38.620 に答える