【发布时间】:2015-06-22 10:50:22
【问题描述】:
我将向我的 Rails 应用程序添加投票系统,我尝试使用 activerecord-reputation-system gem 运行信誉系统,并关注railscasts #364 视频。
我正在为路由错误而苦苦挣扎,当我点击 upvote 或 downvote 时,它显示错误“No route matches [GET] "/haikus/1/vote"
/config/routes.rb:
Youhaiku::Application.routes.draw do
get 'signup', to: 'users#new', as: 'signup'
get 'login', to: 'sessions#new', as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
resources :users
resources :sessions
resources :haikus do
member { post :vote }
end
root to: 'haikus#index'
end
/app/controllers/haikus_controller.rb:
def vote
value = params[:type] == "up" ? 1 : -1
@haiku = Haiku.find(params[:id])
@haiku.add_evaluation(:votes, value, current_user)
redirect_to :back, notice: "Thank you for voting!"
end
/app/views/haikus/_haiku.html.erb:
<div class="haiku">
<%= simple_format haiku.content %>
<em>
-- <%= haiku.user.name %>
| <%= link_to "up", vote_haiku_path(haiku, type: "up"), method: "post" %>
| <%= link_to "down", vote_haiku_path(haiku, type: "down"), method: "post" %>
</em>
</div>
我查看了这个issue,但这对我没有帮助。所以,我真的需要这方面的帮助。
【问题讨论】:
-
试试这个
<%= link_to "up", vote_haiku_path(haiku, type: "up"), method: :post %> -
是的,我试过了,但它不适合我!
-
你能粘贴你在渲染这个链接后得到的html吗?
-
当我点击“Upvote”链接时,出现以下错误 Routing Error No route matches [GET] "/haikus/1/vote" 尝试运行 rake 路由以获取有关可用路由的更多信息。跨度>
-
试试这个:
<a rel="nofollow" data-method="post" href="<%=vote_haiku_path(haiku, type: "up")%>"></a>
标签: ruby-on-rails ruby activerecord