我有一个模型竞赛,允许用户参加和退出比赛。它运行良好,我正在改变网站的其他方面。我再次测试了退出,代码被破坏了。
从竞赛中,当我点击“退出”时,它会带我到一个页面:
No route matches [GET] "/competitions/1/withdraw"
我运行$ rake routes并收到
attend_competition POST /competitions/:id/attend(.:format) competitions#attend
GET /competitions(.:format) competitions#index
POST /competitions(.:format) competitions#create
GET /competitions/new(.:format) competitions#new
edit_competition GET /competitions/:id/edit(.:format) competitions#edit
GET /competitions/:id(.:format) competitions#show
PUT /competitions/:id(.:format) competitions#update
DELETE /competitions/:id(.:format) competitions#destroy
withdraw_competition POST /competitions/:id/withdraw(.:format) competitions#withdraw
GET /competitions(.:format) competitions#index
POST /competitions(.:format) competitions#create
GET /competitions/new(.:format) competitions#new
GET /competitions/:id/edit(.:format) competitions#edit
GET /competitions/:id(.:format) competitions#show
PUT /competitions/:id(.:format) competitions#update
DELETE /competitions/:id(.:format) competitions#destroy
root / 当我取出它时,它会转到网址:http://0.0.0.0:3000/competitions/1/withdraw
我的配置routes.rb文件是
...
resources :competitions, only: [:create, :destroy, :new, :index]
...
resources :competitions do
post 'attend', on: :member
end
resources :competitions do
member do
post 'withdraw'
end
end任何帮助都将不胜感激。
更多信息
因此,我已经验证了我的html应该发送一个post请求。
<% if @competition.users.exclude?(@user) %>
<%= link_to 'Attend Competition', attend_competition_path(@competition.id), :method => :post %>
<% else %>
<%= link_to 'Withdraw', withdraw_competition_path(@competition.id), :method => :post %>他们是发送Get请求的。
我还发现我的服务器无法找到jquery,这是相关的。
application.js
//= require jquery
//= require jquery-ujs
//= require jquery-ui
//= require bootstrap最后,我现在的宝石档案:
gem 'jquery-rails', '2.3.0'
gem 'jquery-ui-rails'发布于 2014-03-02 01:53:58
检查你的HTML。
如果这是一个链接触发取款检查,以确保您有method: :post设置。如果是表格,也要检查一下。无论哪种方式,检查您实际从rails获得的HTML。如果你还不明白,那就把它贴出来吧。
编辑
变化
//= require jquery-ujs至
//= require jquery_ujs发布于 2014-03-02 01:30:58
就像错误说的那样,没有一条路径可以将"/competitions/1/withdraw"与get方法相匹配。您已经在路由中指定了post:
post 'withdraw'如果你也想要一个get请求,你可以这样做:
resources :competitions do
member do
match 'withdraw', via: [:get, :post]
end
end或者,可以在选项哈希中使用method: :post更改链接以发出post请求。有关更多详细信息,请参阅文献资料。
https://stackoverflow.com/questions/22122733
复制相似问题