I have multiple different namespaces like this:
namespace :some_category do
resources :orders
resources :some_foo
get 'aaa', to: 'aa#aa'
get 'bbb', to: 'bb#bb'
# etc
end
namespace :other_namespace do
resources :foo
resources :bar
get 'ccc', to: 'cc#cc'
get 'ddd', to: 'dd#dd'
# etc
end
namespace :etc do
end
Now, the routes are created correctly. Unfortunately, there aren't any routes generated for the namespaces itself. I would like to also have site.com/some_category and site.com/other_namespace.
should I just add outside the namespaces
get 'some_category', to: 'foo#bar'
get 'other_namespace', to: 'foo#baz'
Or is that bad practice?
I would like to make them as some kind of dashboards. Is it possible to generate links to all available subroutes?
I don't like to build a custom dashboard for each namespace. It's not very DRY, because all the dashboards are the same: it is just a list of links, that belongs in that namespace. Is there some code for:
Get the namespace of which I am the dashboard --> get all routes that are inside that namespace --> generate a link for each of those routes
Related
There are a lot of SO questions on this but none of them was able to answer what I was looking for. Please let me know if I am wrong.
I have following routes in my route files
namespace :integrations do
resources :google, only: [] do
collection do
get 'prompt'
get 'callback'
end
end
end
These creates very long url helpers like below.
prompt_integrations_google_index GET /integrations/google/prompt(.:format) integrations/google#prompt
callback_integrations_google_index GET /integrations/google/callback(.:format) integrations/google#callback
I've tried using as but that doesn't work. I don't know if using path is the right way. How can I shorten their helper name?
Ex:
google_prompt_integrations_google_index -> google_prompt
I would like to do the renaming for just one selected route and keep all others unchanged.
You can write the route definition as:
namespace :integrations, as: '' do
resource :google, only: [] do
collection do
get 'prompt'
get 'callback'
end
end
end
And then add the below inflections to remove _index with helper. _index comes from this implementation
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.plural /^(google)$/i, '\1s'
inflect.singular /^(google)s/i, '\1'
end
Now you get as you want:
$ rails routes | grep google
prompt_google GET /integrations/google/prompt(.:format) integrations/googles#prompt
callback_google GET /integrations/google/callback(.:format) integrations/googles#callback
Have you tried this way with as
namespace :integrations, as: '' do
resource :google, only: [:prompt, :callback] do
collection do
get 'prompt', as: 'prompt'
get 'callback', as: 'cb'
end
end
end
On rake routes we get prompt_google and cb_google
#rake routes
prompt_google GET /integrations/google/prompt(.:format) integrations/googles#prompt
cb_google GET /integrations/google/callback(.:format) integrations/googles#callback
Update:
In the case you want to just change 1 route why not have something like
get 'integrations/google/prompt', to: 'integrations/google#prompt', as: 'google_prompt'
rake routes will return
google_prompt GET /integrations/google/prompt(.:format) integrations/google#prompt
I am bit new with Rails (3.) and need your help to use renamed routes. I have following routes to display and search products.
namespace :store do
namespace :product do
resources :home
resources :search
end
end
And rake routes render following output for above resources:
store_product_home_index GET /store/product/home(.:format) store/product/home#index
POST /store/product/home(.:format) store/product/home#create
new_store_product_home GET /store/product/home/new(.:format) store/product/home#new
edit_store_product_home GET /store/product/home/:id/edit(.:format) store/product/home#edit
store_product_home GET /store/product/home/:id(.:format) store/product/home#show
PUT /store/product/home/:id(.:format) store/product/home#update
DELETE /store/product/home/:id(.:format) store/product/home#destroy
store_product_search_index GET /store/product/search(.:format) store/product/search#index
POST /store/product/search(.:format) store/product/search#create
new_store_product_search GET /store/product/search/new(.:format) store/product/search#new
edit_store_product_search GET /store/product/search/:id/edit(.:format) store/product/search#edit
store_product_search GET /store/product/search/:id(.:format) store/product/search#show
PUT /store/product/search/:id(.:format) store/product/search#update
DELETE /store/product/search/:id(.:format) store/product/search#destroy
Instead of having path like /store/product/home, I wanted to rename as /products/home.
So modified routes should look like following:
store_product_home_index GET /products/home(.:format) store/product/home#index
POST /products/home(.:format) store/product/home#create
new_store_product_home GET /products/home/new(.:format) store/product/home#new
edit_store_product_home GET /products/home/:id/edit(.:format) store/product/home#edit
store_product_home GET /products/home/:id(.:format) store/product/home#show
PUT /products/home/:id(.:format) store/product/home#update
DELETE /products/home/:id(.:format) store/product/home#destroy
store_product_search_index GET /products/search(.:format) store/product/search#index
POST /products/search(.:format) store/product/search#create
new_store_product_search GET /products/search/new(.:format) store/product/search#new
edit_store_product_search GET /products/search/:id/edit(.:format) store/product/search#edit
store_product_search GET /products/search/:id(.:format) store/product/search#show
PUT /products/search/:id(.:format) store/product/search#update
DELETE /products/search/:id(.:format) store/product/search#destroy
Note that I am using Rails 3.2.1.
Any help you can provide is greatly appreciated.
As I understand you just want the /store namespace to be removed?
replace
namespace :store do
namespace :product do
resources :home
resources :search
end
end
with
namespace :product do
resources :home
resources :search
end
if you want to keep the namespace for structural reasons try
namespace :store,:path => "" do
namespace :product do
resources :home
resources :search
end
end
hope this helps!
Imagine you are working on a f,acebook(to skip the g,f,w) like site, and you need some routes like:
www.mydomain.com/ihome/jim/posts
www.mydomain.com/ihome/jim/post/3
www.mydomain.com/ihome/jim/posts/3/edit.
Then how to set the routes to get the 'jim' part? I know I can use the following if there is no account part:
namespace :ihome do
resources :posts
end
A quick (untested) answer is : use the scope, it will give you a params[:user]
namespace :ihome do
scope ":user" do
resources :posts
end
end
Have a look at the docs here : http://guides.rubyonrails.org/routing.html#defining-defaults
I'm having a bit of a problem with route namespaces that I've not encountered before. This is actually a part of some gem development I'm doing - but i've reworked the problem to fit with a more generic rails situation.
Basically, I have a namespaced route, but I want it to direct to a generic (top-level) controller.
My controller is PublishController, which handles publishing of many different types of Models - which all conform to the same interface, but can be under different namespaces. My routes look like this:
# config/routes.rb
namespace :manage do
resources :projects do
get 'publish' => 'publish#create'
get 'unpublish' => 'publish#destroy'
end
end
The problem is that this creates the following routes:
manage_project_publish GET /manage/projects/:project_id/publish(.:format) {:controller=>"manage/publish", :action=>"create"}
manage_project_unpublish GET /manage/projects/:project_id/unpublish(.:format) {:controller=>"manage/publish", :action=>"destroy"}
Which is the routes I want, just not mapping to the correct controller. I've tried everything I can think of try and allow for the controller not to carry the namespace, but I'm stumped.
I know that I could do the following:
get 'manage/features/:feature_id/publish' => "publish#create", :as => "manage_project_publish"
which produces:
manage_project_publish GET /manage/projects/:project_id/publish(.:format) {:controller=>"publish", :action=>"create"}
but ideally, I'd prefer to use the nested declaration (for readability) - if it's even possible; which I'm starting to think it isn't.
resource takes an optional hash where you can specify the controller so
resource :projects do
would be written as
resource :projects, :controller=>:publish do
Use scope rather than namespace when you want a scoped route but not a controller within a module of the same name.
If I understand you correct, you want this:
scope :manage do
resources :projects, :only => [] do
get 'publish' => 'publish#create'
get 'unpublish' => 'publish#destroy'
end
end
to poduce these routes:
project_publish GET /projects/:project_id/publish(.:format) {:action=>"create", :controller=>"publish"}
project_unpublish GET /projects/:project_id/unpublish(.:format) {:action=>"destroy", :controller=>"publish"}
Am I understanding your need correctly? If so, this is what Ryan is explaining.
I think what you want is this:
namespace :manage, module: nil do
resources :projects do
get 'publish' => 'publish#create'
get 'unpublish' => 'publish#destroy'
end
end
This does create the named routes as you wish(manage_projects...) but still call the controller ::Publish
It's a bit late but for anyone still struggling with this, you can add a leading slash to the controller name to pull it out of any existing namespace.
concern :lockable do
resource :locks, only: [] do
post "lock" => "/locks#create"
post "unlock" => "/locks#destroy"
end
end
Now if you include that concern anywhere (either namespaced or not) you will always hit the LocksController.
if i use
namespace :helpcenter do
get "hh/logout"
get 'hh/login'
end
it will match the url helpcenter/hh/logout
my question is how to let these method mapping to the url /hh/logout didn't contains the module name
You can use a scope to achieve this:
scope :module => 'helpcenter' do
resources :articles
end
This will generate a mapping for /articles, /articles/new etc and not helpcenter/articles. It will however still route to the articles controller in the helpcenter namespace. e.g.: app/controllers/helpcenter/articles_controller.rb
Hope that helps.