Question

I have a model competitions that allows users to attend and withdraw from a competition. It was working well and I was changing other aspects of the site. I tested the withdraw again and the code is broken.

From a competition when I click withdraw it takes me to a page:

No route matches [GET] "/competitions/1/withdraw"

I ran $ rake routes and received

  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        /  

When I withdraw it goes to the url: http://0.0.0.0:3000/competitions/1/withdraw

my config routes.rb file is

...
resources :competitions, only: [:create, :destroy, :new, :index]
...
resources   :competitions do
  post 'attend', on: :member
end
resources :competitions do
  member do 
    post 'withdraw'
  end
end

any help would be appreciated.

More Info

So, I have verified that my html should be sending a post request

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

And they are send Get requests.

I also found that my server isn't able to find jquery-ujs which is related

application.js

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

and finally my gemfile currently:

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

Pas de solution correcte

Autres conseils

Check your HTML.

If it is a link triggering withdrawals check to make sure you have method: :post set. If it is a form, check that too. Either way, inspect the HTML you are actually getting from rails. If you still can't figure it out go ahead and post it.

Edit

Change

//= require jquery-ujs

To

//= require jquery_ujs

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

Like the error says, there is no route that matches "/competitions/1/withdraw" with a get method. You have specified post in your routes:

post 'withdraw'

If you want a get request as well, you can do this:

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

Or you can change your link to make a post request by using method: :post in the options hash. See the documentation for more details.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top