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
Related
I have some problems with routing custom controller actions
Routes:
resources :clans do
get 'leave' =>'clan#leave_clan'
get 'dismiss' =>'clan#dismiss_clan'
get 'kick_from_clan/:user_id' =>'clan#kick'
get 'invite/:user_id' =>'clan#invite'
get 'join' =>'clan#join'
end
namespace :admin do
resources :clans
resources :users
end
I know i have clans both in admin namespace and also without but that is how i need it, actions are completely different in them.
And I use a generated route as clan_join_path(clan).
This action results in the next error:
uninitialized constant ClanController
Directory structure:
/app
/controller
/admin
/ClansController.rb
/ClansController.rb
EDIT:
Also invite and kick routes are not generated as expected.
*no path* GET /clans/:clan_id/kick_from_clan/:user_id(.:format) clan#kick
*no path* GET /clans/:clan_id/invite/:user_id(.:format) clan#invite
Any suggestion to the edit part?
You're defining a resource called clans and I presume you have a controller also called ClansController (note the puralization). If you don't have this controller, you'll be wanting to create it.
You probably therefore need to pluralize your routes:
resources :clans do
get 'leave' =>'clans#leave_clan'
get 'dismiss' =>'clans#dismiss_clan'
get 'kick_from_clan/:user_id' =>'clans#kick'
get 'invite/:user_id' =>'clans#invite'
get 'join' =>'clans#join'
end
Also make sure your controller is named clans_controller.rb (pluralized).
You can use member do ... end to properly route based on type of action
resources :clans do
member do
get :leave
get :dismiss
#etc
end
end
This will define the routes as clans/:id/leave clans/:id/dismiss
Check if your clans_controller.rb is intentionally not plural.
resources :clans do
get 'leave' =>'clans#leave_clan'
get 'dismiss' =>'clans#dismiss_clan'
get 'kick_from_clan/:user_id' =>'clans#kick'
get 'invite/:user_id' =>'clans#invite'
get 'join' =>'clans#join'
end
app/controllers/clans_controller.rb
class ClansController < ApplicationController
end
I have the following routes:
resources :businesses, only: [:show, :index, :new, :create] do
resources :pledge_drives
end
This creates the following:
% rake routes | grep pledge
business_pledge_drives GET /businesses/:business_id/pledge_drives(.:format) pledge_drives#index
POST /businesses/:business_id/pledge_drives(.:format) pledge_drives#create
new_business_pledge_drife GET /businesses/:business_id/pledge_drives/new(.:format) pledge_drives#new
edit_business_pledge_drife GET /businesses/:business_id/pledge_drives/:id/edit(.:format) pledge_drives#edit
business_pledge_drife GET /businesses/:business_id/pledge_drives/:id(.:format) pledge_drives#show
PUT /businesses/:business_id/pledge_drives/:id(.:format) pledge_drives#update
DELETE /businesses/:business_id/pledge_drives/:id(.:format) pledge_drives#destroy
I do not want the URL helper method to be called business_pledge_drife_path. How can I correct Rails in its singularization of drives? The actual URLs are fine.
Edit: this answer got it right. I added the following to config/initializers/inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural 'drive', 'drives'
inflect.singular 'drives', 'drive'
end
Now my routes are:
business_pledge_drives GET /businesses/:business_id/pledge_drives(.:format) pledge_drives#index
POST /businesses/:business_id/pledge_drives(.:format) pledge_drives#create
new_business_pledge_drive GET /businesses/:business_id/pledge_drives/new(.:format) pledge_drives#new
edit_business_pledge_drive GET /businesses/:business_id/pledge_drives/:id/edit(.:format) pledge_drives#edit
business_pledge_drive GET /businesses/:business_id/pledge_drives/:id(.:format) pledge_drives#show
PUT /businesses/:business_id/pledge_drives/:id(.:format) pledge_drives#update
DELETE /businesses/:business_id/pledge_drives/:id(.:format) pledge_drives#destroy
Most of the time, rails knows about the correct pluralizations of the words, e.g: model name.
However, In other cases, You can define correct conversions in rails using Inflections.
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!
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.