1

I have the following specified in my Rails Routes. I want to allow both GET and POST on this route, but whatever I try, it only allows the #index action, and doesn't access the #create action when a POST is requested.

match ':user_id/special_deals', to: 'special_deals#index'

I've tried this too:

match ':user_id/special_deals', to: 'special_deals#index', :via => [:get, :post]

I need the User ID to be specified first since people with access to the API can access multiple User's info.

1 Answer 1

1

It is working exactly as you asked it to do. If you want POST to routed to create action here are your route configs:

match ':user_id/special_deals', to: 'special_deals#index', :via => [:get]
match ':user_id/special_deals', to: 'special_deals#create', :via => [:post]

There are simpler ways of writing these but I just wanted to use the same format you wrote it. Please check this guide to know about them.

If you already have a User controller, you can write more structured routes like:

resources :users do 
  resources :special_deals, :only => [:index, :create]
end

This will make routes for special_deals like (#shows where it will be routed to):

GET /users/:user_id/special_deals  #special_deals#index
POST /users/:user_id/special_deals  #special_deals#create
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you HungryCode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.