【问题标题】:Rails Route Broke - no code changesRails Route Broke - 无代码更改
【发布时间】:2014-03-02 01:27:09
【问题描述】:

我有一个模型比赛,允许用户参加和退出比赛。它运行良好,我正在更改网站的其他方面。我又测试了withdraw,代码坏了。

从比赛中,当我点击退出时,它会将我带到一个页面:

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-ujs

应用程序.js

//= require jquery
//= require jquery-ujs
//= require jquery-ui
//= require bootstrap

最后是我目前的 gemfile:

gem 'jquery-rails', '2.3.0'
gem 'jquery-ui-rails'

【问题讨论】:

  • 看起来您的路线需要POST,而您正在执行GET。视图或JS有变化吗?
  • 是的,我把它改回了 //=require jquery /n //=require bootstrap,但这并没有解决问题

标签: ruby-on-rails ruby ruby-on-rails-3 routes


【解决方案1】:

检查您的 HTML。

如果是触发提款的链接,请检查以确保您设置了method: :post。如果是表格,也要检查。无论哪种方式,检查您实际从 rails 获得的 HTML。如果您仍然无法弄清楚,请继续发布。

编辑

改变

//= require jquery-ujs

//= require jquery_ujs

https://github.com/rails/jquery-ujs

【讨论】:

    【解决方案2】:

    就像错误所说的那样,没有与 "/competitions/1/withdraw" 匹配的路由与 get 方法。您在路线中指定了post

    post 'withdraw'
    

    如果你也想要获取请求,你可以这样做:

    resources :competitions do
      member do 
        match 'withdraw', via: [:get, :post]
      end
    end
    

    或者您可以通过在选项哈希中使用method: :post 来更改您的链接以发出发布请求。详情请见the documentation

    【讨论】:

    • 发布了有关该问题的更多详细信息。谢谢
    • @Mhsmith21,感谢您提供额外信息。我会更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多