我有一個鏈接控制器,我定義了兩個自定義操作,分別稱爲upvote和downvote。操作定義: -在控制器和添加的路由中定義了一個自定義操作,但仍然沒有路由匹配錯誤。 Rails 5應用程序
def upvote
@link = Link.find_by(id: params[:id])
@link.vote_count += 1
flash[:message]="Vote successfully registered"
redirect_to root_path
end
def downvote
@link = Link.find_by(id: params[:id])
@link.vote_count -= 1
flash[:message]="Vote successfully registered"
redirect_to root_path
end
我已經添加了這些行動路線中的routes.rb文件: -
post '/links/:id', to: 'links#upvote', as: 'upvote'
post '/links/:id', to: 'links#downvote', as: 'downvote'
軌道路線顯示: -
upvote POST /links/:id(.:format) links#upvote
downvote POST /links/:id(.:format) links#downvote
我從視圖中調用動作如下: -
<% if link.user != current_user %>
<span class="vote-buttons">
<div class="btn-group-vertical" role="group" aria-label="vote-buttons">
<%= button_to 'Up', 'upvote_path', type:"button", class:"btn btn-xs btn-success" %>
<%= button_to 'Down', 'downvote_path', type:"button", class:"btn btn-xs btn-danger" %>
</div>
</span>
<% end %>
但是我不斷收到此錯誤: -
No route matches [POST] "/upvote_path"
爲什麼我看到這個,即使軌道路線顯示路線存在?
我也試過重新啓動服務器。
你定義它作爲一個POST請求那你爲什麼包括:ID的路線,你可以簡單地定義它像這樣=>郵報「/鏈接」,到: 'links#upvote',如:'upvote'並像upvote_path一樣使用它 – Navin
我需要使用upvote/downvote操作中的params鏈接的id。 – RawCode
所以讓它獲得GET請求,像這樣=> get'/ links /:id',to:'links#upvote',as:'upvote'並且像這樣使用它=> upvote_path(id:link.id) – Navin