我已经通过Thumbs_Up宝石在我的应用程序中建立了投票。它在我的localhost上运行得很好,但是当我试图在Heroku上投票时,我的Heroku日志中出现了这个错误:
ActionController::RoutingError (No route matches [GET] "/jokes/wise-police-officer/vote_up"):下面是我的jokes模型的路线:
resources :jokes do
patch :approve, on: :member
patch :reject, on: :member
member do
post :vote_up
post :vote_down
end
end同样,这很奇怪,因为它在localhost中运行得很好。有人有类似的问题吗?
下面是投票链接的HTML/ERB:
<div class="width: 100%"><%= link_to " ", vote_up_joke_path(joke), class: 'upvote glyphicon glyphicon-chevron-up', method: :post, style: "margin-right: 0; margin-left: 0" %></div>
<div class="width: 100%"><h3 style="margin-top: 0; margin-bottom: 0"><strong><%= joke.plusminus %></strong></h3></div>
<div class="width: 100%"><%= link_to " ", vote_down_joke_path(joke), class: 'downvote glyphicon glyphicon-chevron-down', method: :post, style: "margin-right: 0; margin-left: 0" %></div>下面是我的bundle exec rake routes输出的全部内容:
Prefix Verb URI Pattern Controller#Action
ckeditor /ckeditor Ckeditor::Engine
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user GET /users/:id(.:format) users#show
home_index GET /home/index(.:format) home#index
root GET / home#index
home_lifehacks GET /home/lifehacks(.:format) home#lifehacks
home_mangear GET /home/mangear(.:format) home#mangear
home_gifthelper GET /home/gifthelper(.:format) home#gifthelper
home_commandments GET /home/commandments(.:format) home#commandments
gifts_babies GET /gifts/babies(.:format) gifts#babies
gifts_kids GET /gifts/kids(.:format) gifts#kids
gifts_teens GET /gifts/teens(.:format) gifts#teens
gifts_adults GET /gifts/adults(.:format) gifts#adults
principles GET /principles(.:format) principles#index
principles_intro GET /principles/intro(.:format) principles#intro
principles_intermediate GET /principles/intermediate(.:format) principles#intermediate
principles_advanced GET /principles/advanced(.:format) principles#advanced
principles_beef GET /principles/beef(.:format) principles#beef
principles_chicken GET /principles/chicken(.:format) principles#chicken
principles_pork GET /principles/pork(.:format) principles#pork
principles_fish GET /principles/fish(.:format) principles#fish
principles_recipes GET /principles/recipes(.:format) principles#recipes
archive_suggestion PATCH /suggestions/:id/archive(.:format) suggestions#archive
unarchive_suggestion PATCH /suggestions/:id/unarchive(.:format) suggestions#unarchive
suggestions GET /suggestions(.:format) suggestions#index
POST /suggestions(.:format) suggestions#create
suggestion DELETE /suggestions/:id(.:format) suggestions#destroy
suggestions_archives GET /suggestions/archives(.:format) suggestions#archives
man_tests POST /man_tests(.:format) man_tests#create
new_man_test GET /man_tests/new(.:format) man_tests#new
man_tests_fail GET /man_tests/fail(.:format) man_tests#fail
approve_joke PATCH /jokes/:id/approve(.:format) jokes#approve
reject_joke PATCH /jokes/:id/reject(.:format) jokes#reject
vote_up_joke POST /jokes/:id/vote_up(.:format) jokes#vote_up
vote_down_joke POST /jokes/:id/vote_down(.:format) jokes#vote_down
jokes GET /jokes(.:format) jokes#index
POST /jokes(.:format) jokes#create
new_joke GET /jokes/new(.:format) jokes#new
edit_joke GET /jokes/:id/edit(.:format) jokes#edit
joke GET /jokes/:id(.:format) jokes#show
PATCH /jokes/:id(.:format) jokes#update
PUT /jokes/:id(.:format) jokes#update
DELETE /jokes/:id(.:format) jokes#destroy
home_jokes GET /home/jokes(.:format) home#jokes
otherjokes_kids GET /otherjokes/kids(.:format) otherjokes#kids
otherjokes_mixed GET /otherjokes/mixed(.:format) otherjokes#mixed
otherjokes_men GET /otherjokes/men(.:format) otherjokes#men
otherjokes_comeback GET /otherjokes/comeback(.:format) otherjokes#comeback
approve_recipe PATCH /recipes/:id/approve(.:format) recipes#approve
reject_recipe PATCH /recipes/:id/reject(.:format) recipes#reject
vote_up_recipe POST /recipes/:id/vote_up(.:format) recipes#vote_up
vote_down_recipe POST /recipes/:id/vote_down(.:format) recipes#vote_down
recipes GET /recipes(.:format) recipes#index
POST /recipes(.:format) recipes#create
new_recipe GET /recipes/new(.:format) recipes#new
edit_recipe GET /recipes/:id/edit(.:format) recipes#edit
recipe GET /recipes/:id(.:format) recipes#show
PATCH /recipes/:id(.:format) recipes#update
PUT /recipes/:id(.:format) recipes#update
DELETE /recipes/:id(.:format) recipes#destroy
home_bbq GET /home/bbq(.:format) home#bbq
other_recipes_all GET /other_recipes/all(.:format) other_recipes#all
other_recipes_beef GET /other_recipes/beef(.:format) other_recipes#beef
other_recipes_chicken GET /other_recipes/chicken(.:format) other_recipes#chicken
other_recipes_fish GET /other_recipes/fish(.:format) other_recipes#fish
other_recipes_other_meat GET /other_recipes/other_meat(.:format) other_recipes#other_meat
other_recipes_veggies GET /other_recipes/veggies(.:format) other_recipes#veggies
approve_rule PATCH /rules/:id/approve(.:format) rules#approve
reject_rule PATCH /rules/:id/reject(.:format) rules#reject
vote_up_rule POST /rules/:id/vote_up(.:format) rules#vote_up
vote_down_rule POST /rules/:id/vote_down(.:format) rules#vote_down
rules GET /rules(.:format) rules#index
POST /rules(.:format) rules#create
new_rule GET /rules/new(.:format) rules#new
edit_rule GET /rules/:id/edit(.:format) rules#edit
rule GET /rules/:id(.:format) rules#show
PATCH /rules/:id(.:format) rules#update
PUT /rules/:id(.:format) rules#update
DELETE /rules/:id(.:format) rules#destroy
reminders GET /reminders(.:format) reminders#index
POST /reminders(.:format) reminders#create
new_reminder GET /reminders/new(.:format) reminders#new
edit_reminder GET /reminders/:id/edit(.:format) reminders#edit
reminder GET /reminders/:id(.:format) reminders#show
PATCH /reminders/:id(.:format) reminders#update
PUT /reminders/:id(.:format) reminders#update
DELETE /reminders/:id(.:format) reminders#destroy
Routes for Ckeditor::Engine:
pictures GET /pictures(.:format) ckeditor/pictures#index
POST /pictures(.:format) ckeditor/pictures#create
picture DELETE /pictures/:id(.:format) ckeditor/pictures#destroy
attachment_files GET /attachment_files(.:format) ckeditor/attachment_files#index
POST /attachment_files(.:format) ckeditor/attachment_files#create
attachment_file DELETE /attachment_files/:id(.:format) ckeditor/attachment_files#destroy发布于 2016-10-17 21:51:30
我联系了heroku支持部门,他们在推荐我使用AJAX时得到了与@AjitSingh类似的答复:
我明白你说的了。与其依赖标记,我建议使用按钮或表单进行AJAX调用来完成适当的POST请求。
不过,AJAX现在比我的技术水平要高一点。我还在ROR API上找到了这个关于使用link_to的小宝石(不是双关语)。
方法: HTTP谓词的符号-此修饰符将动态创建HTML表单,并立即提交表单,以便使用指定的HTTP谓词进行处理。对于让链接在危险的操作中执行POST操作很有用,比如删除一个记录(搜索机器人可以在你的站点上执行搜索操作)。受支持的动词有:post、:delete、:修补程序和:put。注意,如果用户禁用了JavaScript,请求将返回到使用GET。如果使用了href:'#‘,并且用户禁用了JavaScript,单击该链接将没有任何效果。如果您依赖POST行为,则应该使用request对象的post、delete、修补程序或put方法,在控制器的操作中检查它。
最后,我咨询了this的建议,所以发布并使用了button_to而不是link_to,解决了这个问题。呼。
发布于 2016-10-13 17:57:41
它提交了一个“get”请求,并且需要提交一个post请求。确保在处理“向上投票”的method: :post调用中指定了link_to。
发布于 2016-10-13 18:23:06
确保在你的application.js中有跟随者
//= require jquery
//= require jquery_ujsjquery-ujs从超链接发出非获取请求。即使link_to方法使用data-method=post生成适当的html,您也需要jquery-ujs来发出非GET请求,其他明智的方法是,您的链接自然会创建GET请求。
https://stackoverflow.com/questions/40027772
复制相似问题