Ruby-on-Rails 投票部分路由
Ruby-on-Rails Voting Partial Routing
我的应用程序有投票功能,但无法返回到已应用投票的同一页面。我知道我必须填写 link_to 方法,但我对 routing/ruby 语法的理解有点有限,所以我什至不确定 [post, vote] 是否正确。我觉得我还缺少其他东西。我是否提供了足够的信息?我应该如何处理这个问题?或者更好的是,我怎样才能更好地理解路由?谢谢。
这是我遇到的错误:
No route matches [GET] "/posts/13/up-vote"
我的部分投票:
<% if policy( Vote.new ).create? %>
<div class="vote-arrows pull-left">
<div>
<%= link_to [post, vote],
post_up_vote_path(post),
class: "glyphicon glyphicon-chevron-up #{(current_user.voted(post) && current_user.voted(post).up_vote?) ? 'voted' : '' }" %>
</div>
<div>
<strong><%= post.points %></strong>
</div>
<div>
<%= link_to [post, vote],
post_down_vote_path(post),
class: "glyphicon glyphicon-chevron-down #{(current_user.voted(post) && current_user.voted(post).down_vote?) ? 'voted' : '' }" %>
</div>
</div>
<% end %>
我的routes.rb
Bloccit::Application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index]
end
resources :posts, only: [] do
resources :comments, only: [:create, :destroy]
resources :favorites, only: [:create, :destroy]
post '/up-vote' => 'votes#up_vote', as: :up_vote
post '/down-vote' => 'votes#down_vote', as: :down_vote
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
查看错误消息:No route matches [GET] "/posts/13/up-vote"
。它正在寻找 [GET]
路线,但您已在 config/routes.rb
文件中定义了 [POST]
路线。
您需要将 method: :post
添加到您的两个 link_to
助手中才能触发 [POST]
请求。这是它的样子:
<%= link_to [post, vote], post_down_vote_path(post), class: "glyphicon glyphicon-chevron-down #{(current_user.voted(post) && current_user.voted(post).down_vote?) ? 'voted' : '' }", method: :post %>
希望对您有所帮助!
我的应用程序有投票功能,但无法返回到已应用投票的同一页面。我知道我必须填写 link_to 方法,但我对 routing/ruby 语法的理解有点有限,所以我什至不确定 [post, vote] 是否正确。我觉得我还缺少其他东西。我是否提供了足够的信息?我应该如何处理这个问题?或者更好的是,我怎样才能更好地理解路由?谢谢。
这是我遇到的错误:
No route matches [GET] "/posts/13/up-vote"
我的部分投票:
<% if policy( Vote.new ).create? %>
<div class="vote-arrows pull-left">
<div>
<%= link_to [post, vote],
post_up_vote_path(post),
class: "glyphicon glyphicon-chevron-up #{(current_user.voted(post) && current_user.voted(post).up_vote?) ? 'voted' : '' }" %>
</div>
<div>
<strong><%= post.points %></strong>
</div>
<div>
<%= link_to [post, vote],
post_down_vote_path(post),
class: "glyphicon glyphicon-chevron-down #{(current_user.voted(post) && current_user.voted(post).down_vote?) ? 'voted' : '' }" %>
</div>
</div>
<% end %>
我的routes.rb
Bloccit::Application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index]
end
resources :posts, only: [] do
resources :comments, only: [:create, :destroy]
resources :favorites, only: [:create, :destroy]
post '/up-vote' => 'votes#up_vote', as: :up_vote
post '/down-vote' => 'votes#down_vote', as: :down_vote
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
查看错误消息:No route matches [GET] "/posts/13/up-vote"
。它正在寻找 [GET]
路线,但您已在 config/routes.rb
文件中定义了 [POST]
路线。
您需要将 method: :post
添加到您的两个 link_to
助手中才能触发 [POST]
请求。这是它的样子:
<%= link_to [post, vote], post_down_vote_path(post), class: "glyphicon glyphicon-chevron-down #{(current_user.voted(post) && current_user.voted(post).down_vote?) ? 'voted' : '' }", method: :post %>
希望对您有所帮助!