我的应用程序上有投票功能,但无法通过投票返回同一页面。我知道我必须填写link_to方法,但我对路由/ 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 %>
my 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
答案 0 :(得分:1)
查看错误消息:No route matches [GET] "/posts/13/up-vote"
。它正在寻找[GET]
路由,但您已在[POST]
文件中定义了config/routes.rb
路由。
您需要向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 %>
个请求。这就是它的样子:
Error: [$parse:syntax] http://errors.angularjs.org/1.3.15/$parse/syntax?p0=Jun&p1=is%20an%20unexpected%20token&p2=5&p3=Mon%20Jun%2008%202015%2013%3A37%3A02%20GMT-0400%20(EDT)&p4=Jun%2008%202015%2013%3A37%3A02%20GMT-0400%20(EDT)
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js?ver=1.3.15:6:417
at ib.throwError (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js?ver=1.3.15:191:165)
at ib.parse (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js?ver=1.3.15:189:457)
at $get (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js?ver=1.3.15:110:285)
at https://workland.ca/wp-content/themes/upc/bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js?ver=1.0:8:23047
at Object.r [as forEach] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js?ver=1.3.15:7:302)
at link (https://workland.ca/wp-content/themes/upc/bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js?ver=1.0:8:22940)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js?ver=1.3.15:70:141
at $ (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js?ver=1.3.15:70:197) <input type="text" class="form-control ng-pristine ng-untouched ng-valid ng-isolate-scope" ng-model="$data" datepicker-popup="dd-MMMM-yyyy" datepicker-mode="day" show-weeks="true" starting-day="0" init-date="Mon Jun 08 2015 13:37:02 GMT-0400 (EDT)" min-mode="day" max-mode="year" format-day="dd" format-month="MMMM" format-year="yyyy" format-day-header="EEE" format-day-title="MMMM yyyy" format-month-title="yyyy" year-range="20" show-button-bar="true" current-text="Today" clear-text="Clear" close-text="Done" close-on-date-selection="true" date-picker-append-to-body="false">
希望这有帮助!