How to make a route both for get and post? - ruby-on-rails

I have this:
resources :users do
collection do
get 'blah'
end
end
I want to make this action (blah) for both post and get now, possible?

Looks like verb constraints is what you want.
match 'blah', to: 'users#blah', via: [:get, :post]
or
resources :users do
collection do
match 'blah', via: [:get, :post]
end
end

You could just enter the same name for the post route like this:
resources :users do
collection do
get 'blah'
post 'blah'
end
end
Both routes will have the same controller, action and url_helpers

Related

How do I create custom routes in Ruby on Rails - 4.2

Right now I have the route as below;
resources :invoices do
collection do
match 'search' => 'invoices#search', via: [:get, :post], as: :search
end
end
But I would like to get 'deliveries#search' instead of 'invoices#search'.
I successfully customized for the below routes;
get 'deliveries', to: 'invoices#index', as: :deliveries
get 'deliveries/new', to: 'invoices#new', as: :delivery
get 'deliveries/:id/edit', to: 'invoices#edit', :as => :del
I have tried but unable to customized for the 'deliveries#search'.
Any suggestions are most welcome.
Thank you in advance.
resources :invoices, path: 'deliveries' do
collection do
match 'search' => 'invoices#search', via: [:get, :post], as: :search
end
end
it will generate route with prefix deliveries like /deliveries/your_action
Change your routes as follows
resources :deliveries, controller: 'invoices', only: [:index, :new, :edit]
resources :deliveries, only: [] do
collection do
get :search
post :search
end
end
This will generate following routes for you:
deliveries GET /deliveries(.:format) invoices#index
new_delivery GET /deliveries/new(.:format) invoices#new
edit_delivery GET /deliveries/:id/edit(.:format) invoices#edit
search_deliveries GET /deliveries/search(.:format) deliveries#search
POST /deliveries/search(.:format) deliveries#search
EDIT
According to your comment I think you just need to use different controller
Try this
resources :deliveries, controller: 'invoices', only: [:index, :new, :edit] do
collection do
get :search
post :search
end
end
This will generate following routes:
search_deliveries GET /deliveries/search(.:format) invoices#search
POST /deliveries/search(.:format) invoices#search
deliveries GET /deliveries(.:format) invoices#index
new_delivery GET /deliveries/new(.:format) invoices#new
edit_delivery GET /deliveries/:id/edit(.:format) invoices#edit

Is there a method to change URL using namespaces?

I'm trying to change the URL name.
This is my routes.rb:
namespace :user_management do
resources :user do
collection do
get 'main'
end
end
end
match ':controller(/:action(/:id(.:format)))', :via => [:get, :post]
Rake routes:
Prefix Verb URI Pattern Controller#Action
root GET / user_management/login#login
user_main_user_management_user_index GET /user_management/user/main(.:format) user_management/user#main
user_management_user_index GET /user_management/user(.:format) user_management/user#index
POST /user_management/user(.:format) user_management/user#create
new_user_management_user GET /user_management/user/new(.:format) user_management/user#new
edit_user_management_user GET /user_management/user/:id/edit(.:format) user_management/user#edit
user_management_user GET /user_management/user/:id(.:format) user_management/user#show
PATCH /user_management/user/:id(.:format) user_management/user#update
PUT /user_management/user/:id(.:format) user_management/user#update
DELETE /user_management/user/:id(.:format) user_management/user#destroy
GET|POST /:controller(/:action(/:id(.:format))) :controller#:action
My URL is:
localhost/user_management/user/main
And I want:
localhost/user_main
I tried this but it is not working:
namespace :user_management do
resources :user do
collection do
get 'main', as: :user_main
end
end
end
I tried this but is not working either:
namespace :user_management do
resources :user do
collection do
get '/user_main', as: "user_management/user#main
end
end
end
try
match "user_admin", :to => "user_management/user#main"
you can do like in example:
get 'exit', to: 'sessions#destroy', as: :logout # url => /logout
FROM RAILS ROUTING

Remove controller from friendly URL

I have written freindly URLs for the show action of the School Resource but now have
before i had ;
http://webaddress/schools/2
and now i have;
http://webaddress/schools/school_name
However, i want
http://webaddress/school_name
My config routes look like this for the resource;
resources :schools do
collection do
match 'search' => 'schools#search', via: [:get, :post], as: :search
end
end
How can i achieve that? thank you
Add this at the last your routes file:
match ':id' => 'schools#show', via: [:get]
A more conventional way would be to use the path: option in your route resources, like this:
#config/routes.rb
...
resources :schools, path: "", only: :show #-> has to go at end of file!
This will give you the ability to add different methods to this, as well as keeping with Rails conventions :)

Abstracting rails route

I want to replace the normal /users/:id route that is created by the resources command, with a more abstract /profile route. It won't be possible to view other users profiles in my app, and therefor the current route is unnecessary specific.
I have tried to overwrite the route created by 'resources :users' with:
get '/profile', to: 'users#show'
and other variances and combinations, but can't seem to get it right. Either the controller can't find the user because of a missing id or it simply can't find the route.
Thanks for the help!
You can use this code in routes.rb file:
resources :users, :except => :show
collection do
get 'profile', :action => 'show'
end
end
It will generate url "/users/profile".
But, if u want to use only '/profile', then don't create route as collection inside users resources block.
resources :users, :except => :show
get 'profile' => "users#show", :as => :user_profile
It will redirect '/profile' to show action in users controller.
I suggest simply adding a users/me route pointing to the show action of your UsersController like so:
resources :users, only: [] do
collection do
get 'me', action: :show
end
end
You can also use the match keyword in routes.rb file.
match 'users/:id' => 'users#show', as: :user_profile, via: :get

Routes with optional params in Rails

I'm trying to setup a route that looks like this: acme.com/posts/:category/:status. Both :category and :status are optional. I wrote many variations, but none worked:
resources :posts do
match '(/:category)(/:status)', to: 'posts#index', as: 'filter', on: :collection
end
# Category Links
link_to "Questions", filter_posts_path(:questions)
link_to "Suggestions", filter_posts_path(:suggestions)
# Status Links
link_to "Published", filter_posts_path(params[:category], :published)
link_to "Draft", filter_posts_path(params[:category], :draft)
The idea is to be able to 1) filter by category, 2) filter by status, and 3) filter by both category and status if both are available. The current setup has also broken my /posts/new path, always redirecting to posts#index.
I had this variation and it seems working fine:
namespace :admin do
resources :posts, :except => [:show] do
collection do
get "/(:category(/:status))", to: "posts#index", as: "list", :constraints => lambda{|req|
req.env["PATH_INFO"].to_s !~ /new|\d/i
}
end
end
end
= CONTROLLER=admin/posts rake route
list_admin_posts GET /admin/posts(/:category(/:status))(.:format) admin/posts#index
You can use the more RESTful resources :posts (in config/routes.rb) and send the params in the query string.
With that approach, all parameters are optional and you're not limited to using predefined parameters.
Do this works for you?
resources :posts do
collection do
match '/:category(/:status)', to: 'posts#index', as: 'filter'
match '/:status', to: 'posts#index', as: 'filter'
end
end
Hope at least it helps!
You could try something like this:
match '/filter/*category_or_status' => 'posts#index', as: 'filter'
With this you can build your own filter path. Then you could parse params[:category_or_status] in your controller and get the category or status if they are given.

Resources