簡體   English   中英

自定義 Rails 投票系統路由不起作用

[英]Custom Rails Voting System Routes Not Working

嗨,我有一個包含用戶、問題、響應和投票模型的小應用程序:

class Vote < ApplicationRecord
  belongs_to :user
  belongs_to :response

  enum vote_type: [ :upvote, :downvote]

  validates :response, uniqueness: { scope: :user }
  validates :user, uniqueness: { scope: :response }

end


class Response < ApplicationRecord
  belongs_to :question
  belongs_to :user
  has_many :votes
end


class Question < ApplicationRecord
    belongs_to :user
    has_many :responses
end

我想創建一個鏈接來支持和反對響應

所以在 app/controllers/questions/responses/votes_controller.rb

class Questions::Responses::VotesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_response


  def upvote
    @user = current_user
    vote = Vote.new
    vote.response = @response
    vote.user = @user
    if vote.save
      vote.upvote!
      redirect_to @response.question, notice: 'Response was successfully Upvoted.' 
    else
      redirect_to @response.question, notice: 'No Vote' 
    end
  end

  def downvote
    @user = current_user
    vote = Vote.new
    vote.response = @response
    vote.user = @user
    if vote.save
      vote.downvote!
      redirect_to @response.question, notice: 'Response was successfully DownVote.'
    else
      redirect_to @response.question, notice: 'No Vote' 
    end
  end

  private

  def set_response
    @response = Response.find(params[:response_id])
  end

end

我創建了路線

resources :questions do
    resources :responses, module: :questions do
      resources :votes, module: :responses, only: [:upvote, :downvote]
    end
  end

但這不起作用

當我刪除“, only: [:upvote, :downvote]”

它創建路線

question_response_votes GET    /questions/:question_id/responses/:response_id/votes(.:format)                           questions/responses/votes#index
                                      POST   /questions/:question_id/responses/:response_id/votes(.:format)                           questions/responses/votes#create
           new_question_response_vote GET    /questions/:question_id/responses/:response_id/votes/new(.:format)                       questions/responses/votes#new
          edit_question_response_vote GET    /questions/:question_id/responses/:response_id/votes/:id/edit(.:format)                  questions/responses/votes#edit
               question_response_vote GET    /questions/:question_id/responses/:response_id/votes/:id(.:format)                       questions/responses/votes#show
                                      PATCH  /questions/:question_id/responses/:response_id/votes/:id(.:format)                       questions/responses/votes#update
                                      PUT    /questions/:question_id/responses/:response_id/votes/:id(.:format)                       questions/responses/votes#update
                                      DELETE /questions/:question_id/responses/:response_id/votes/:id(.:format)                       questions/responses/votes#destroy

我只想投贊成票或反對票……有人能告訴我怎么做嗎? 或者我做錯了什么?

更新

在以下回復的幫助下,我找到了正確的路線

resources :questions do
    resources :responses, module: :questions do
      get 'upvote', controller: 'responses/votes', as: :upvote
      get 'downvote', controller: 'responses/votes', as: :downvote
      resource :favourite, module: :responses, only: [:create, :destroy]
    end
  end

與鏈接

<%= link_to question_response_upvote_path(response.question.id ,response) %>

<%= link_to question_response_downvote_path(response.question.id ,response) %>

看起來您實際上還沒有為 upvote 或 downvote 創建自定義路由,您只指定了應該為控制器中的哪些操作提供資源。

因此,當您運行命令rake routes時,您需要在routes.rb中以在 POST 下指定的相同格式創建一個 POST rake routes

像這樣:

post '/questions/:question_id/responses/:response_id/upvote' => 'votes_controller#upvote', :as => :upvote

:as => :upvote創建upvote_path ,然后您可以<%= link_to %>或添加到 form_for 中,但是您選擇在您的視圖中引用 upvotes。

在@crachtors 回復的幫助下,我找到了正確的路線

resources :questions do
    resources :responses, module: :questions do
      get 'upvote', controller: 'responses/votes', as: :upvote
      get 'downvote', controller: 'responses/votes', as: :downvote
      resource :favourite, module: :responses, only: [:create, :destroy]
    end
  end

與鏈接

<%= link_to question_response_upvote_path(response.question.id ,response) %>

<%= link_to question_response_downvote_path(response.question.id ,response) %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM