Rails routes.rb use get to add suffix to path - ruby-on-rails

I'm trying to figure out how can i build this route in rails:
resources :cases do
resource :profile do
get 'regions', :to => "cases#regions"
end
end
This code will generate method case_regions_profile_path, but i want it upside down: case_profile_regions_path, is it possible using get 'rule'? I just want to point this path to a controller with specific action.

Its typo you have used resource instead of resources for profiles
resources :cases do
resources :profile do
get 'regions', :to => "cases#regions"
end
end
This generates:
case_profile_regions GET /cases/:case_id/profile/:profile_id/regions(.:format) cases#regions

Related

Placing User ID's within Routes with Devise

I currently have a default devise set up and wanting to put the user id's within my logged in routes and leave the others alone such as Welcome,FAQ, About us etc.
Such as
www.example.com/user-id/offers
My Current Routes File
devise_for :users
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :offers do
member do
put :tourcomplete
end
end
resources :categories, only: :show
root 'welcome#index'
get '/balance', to: 'balance#show', as: 'balance'
patch '/balance', to: 'balance#paypal', as: 'balance_paypal'
patch '/withdraw', to: 'balance#withdraw', as: 'balance_withdraw'
I can't find any docs on this and my previous answer to a similar question was very vague and not helpful.
Thanks
You will need to use a nested routes:
resources :users do
resources :offers do
member do
put :tourcomplete
end
end
end
so now your routes will be /users/:id/offers, run rake routes to check your routes.
if you want to exclude users then you will have to specify the route yourself:
match ':user_id/offers' => 'offers#index'

How to create clean URL in rails nested resource?

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

Setting up routes in rails

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

Upgrading routes in Rails 3

Currently I have something like this:
resources :books do
collection do
get 'search'
end
end
and my controller name is also "books" and I have a action method called "search" inside it
I would like that "get search" part to also be a resource, kind of like nested resources... but I don't want to break other peoples codes that are using the current route that this generate, so need to update it in a passive way!
resources :books do
collection do
get 'search'
end
resources :searches
end
...if I'm understanding you correctly, that should be what you want. It won't break other routes, just add new ones.
Run rake routes to make sure you have all the routes you want/need.
Use shallow routes nesting like:
resources :books , :shallow => true do
resources :searches
end
Now you will get the following routes:
/books/1 => books_path(1)
/books/1/searches => books_searches_index_path(1)
/searches/2 => searches_path(2)
Similarly you can get separate routing for defined routes like:
get '(:books)/searches', :to => 'books#index'

Rails: How to set params on some paths with scope and set them to nil on some others?

I'm using some routes with scopes, and some without. See below.
resources :cities
resources :categories
devise_for :clients
namespace :clients do
resources :account
resources :dashboard
resources :offers
end
scope "/:current_city" do
scope "/:current_category" do
match 'articles/last_articles' => 'articles#index_last_articles', :as => "last_articles"
resources :articles do
resources :comments
end
end
end
root :to => "home#index"
I am using that params :current_city and :current_category and it gives me a URL like
http://localhost:3000/warszawa/all/articles/last_articles when I'm accessing articles.
**PROBLEM**
I have now such a problem that if I click on a link_to cities_path or root_path, then it adds those two parameters to the URL as http://localhost:3000/?current_category=all&current_city=warszawa.
I don't want these two parameters destroying the beauty of my URL :o(
The only way I found was to pass :current_city => nil, :current_category => nil for each link, but that's really heavy. I tried also the same but in my routes, which is working for normal resources, but not for namespaces, root_path, devise_for routes, and to be honest, that looks horrible in the routes.rb.
**QUESTIONS**
First I do not understand why these params are passed everywhere if I ask them only in the section with scope?!
Secondly, is there a way to make it work like I want or I should modify my routes?
I wish you understand my problem and if you have any comment or idea, please do not hesitate!
Thx

Resources