When I put the following code in my routes config :
resources :users do
end
I get all the CRUD operations routes. i.e
/users/new
/users/:id/edit
and so on.
How do I configure routes so I get route like this :
/users/lookup/:search_query
And when users reaches this routes he/she should be taked to lookup method of my controller
I would do :
resources :users do
get :lookup, on: :collection
end
And I would pass the search_query as a parameter. With that you will be more flexible.
resources :users do
get '/lookup/:search_query' => 'users#lookup', on: :collection
end
resources :users do
end
match '/users/lookup/:search_query' => "users#lookup", :as => :user_lookup
Related
I have the following Rails 5 resources, and I want the URL to show /welcome on huddles#index (as well as for the huddles_path url helper). e.g, I don't want huddles_welcome_path. Can this be done? Thank you!
resources :huddles do
get '/welcome', to: 'huddles#index'
resources :invitations
end
Move the route outside of the huddles resource if you don't want to include huddles/ in the route:
resources :huddles do
resources :invitations
end
get '/welcome', to: 'huddles#index'
resources :huddles do
get '/welcome', to: 'huddles#index', :as => :huddles_path
resources :invitations
end
Just Add a :as => :namethatyouwant
I get the following clicking that link
<%= link_to "(#{User.count_friend_requests(current_user)}) Friends Requests", :controller => "users", :action=>"friend_requests"%>
i get that error
ActionController::UrlGenerationError in Devise::Registrations#edit
No route matches {:action=>"friend_requests", :controller=>"devise/users", :format=>"1"}
routes
devise_for :users, :path_prefix => 'my'
resources :users, :only => [:friend_requests] do
get "friend_requests", :on => :collection
end
users_controller
def friend_requests
#frnds = User.find_friend_requests(current_user)
end
Heh, you've mounted devise with prefix /my/, so your routes are like
/my/users/..
and resources :users mounts your code without any prefix.
You should remove prefix my or or change your resources to
scope "my" do
resources :users, :only => [:friend_requests] do
get "friend_requests", :on => :collection
end
end
to get path like /my/users/friend_requests/
Also I see that rails looks for your method in devise/users (not in your users) controller
No route matches {:action=>"friend_requests", :controller=>"devise/users", :format=>"1"}
If you want to patch it, you should take a look how to patch a devise's users controller.
I have two controller Stores and Stocks and routes for these two controller is given below:
resources :stores do
resources :stocks,param: :product_id,:only=>[:index] do
get '/:product_id', to: 'stocks#index'
end
end
After rake routes I'm getting the path like:
GET /stores/:store_id/stocks/:stock_id/:product_id(.:format)
But I want to remove :stock_id from that path so that the resultant path will be:
GET /stores/:store_id/stocks/:product_id(.:format)
If it's possible then please help.
Try this:
resources :stores do
resources :stocks, except: :index do
get '/:product_id', to: 'stocks#index', on: :collection
end
end
This will give you:
GET /stores/:store_id/stocks/:product_id(.:format) stocks#index
if you want default index action also then:
resources :stores do
resources :stocks do
get '/:product_id', to: 'stocks#index', on: :collection
end
end
This will give you both index:
GET /stores/:store_id/stocks/:product_id(.:format) stocks#index
store_stocks GET /stores/:store_id/stocks(.:format) stocks#index
I want to route http://localhost:3000/users/1/rename/alex to my users controller with rename action.
what I did was:
match 'users/:id/rename/:name' => 'users#rename', but this is not working, the part after 'users/:id/' is not mapped at all, since I cannot get name by params[:name]
Update:
In routes.rb
resources :users do
put 'rename/:code', :action => :rename, :code => /\w{5}/, :on => :member
end
and,
$ rake routes
...
PUT /users/:id/rename/:code(.:format) {:code=>/\w{5}/, :action=>"rename", :controller=>"users"}
...
If you have resources :users, put your match line before it.
Alternatively, you can pass a block to resources:
resources :users do
match 'rename/:name' => 'users#rename', :on => :member
end
I have a few reoccurring patterns in my routes.rb and I would like to make it DRY by creating a method that creates those routes for me.
An example of what I want to accomplish can be seen in the Devise gem where you can use the following syntax:
#routes.rb
devise_for :users
Which will generate all the routes necessary for Devise. I would like to create something similar. Say for example that I have the following routes:
resources :posts do
member do
get 'new_file'
post 'add_file'
end
match 'files/:id' => 'posts#destroy_file', :via => :delete, :as => :destroy_file
end
resources :articles do
member do
get 'new_file'
post 'add_file'
end
match 'files/:id' => 'articles#destroy_file', :via => :delete, :as => :destroy_file
end
This starts getting messy quite quickly so I would like to find a way to do it like this instead:
resources_with_files :posts
resources_with_files :articles
So my question is, how can I create the resources_with_files method?
Put this in something like lib/routes_helper.rb:
class ActionDispatch::Routing::Mapper
def resources_with_files(*resources)
resources.each do |r|
Rails.application.routes.draw do
resources r do
member do
get 'new_file'
post 'add_file'
delete 'files' => :destroy_file
end
end
end
end
end
end
and require it in config/routes.rb