rails routing, add path name also keep old urls working - ruby-on-rails

in my routes file, when i change
resources :foobar
to
resources :foobars, path: "foo-bars"
the urls become example.com/foo-bars, example.com/foo-bars/1 etc.
this is ok.
but how can i also keep the old urls, example.com/foobars, example.com/foobar/3 also working?
i know, i can hardcode it,
get "foobars", to: 'foobar#index'
get "foobar/:id", to: 'foobar#show'
...
but is there a clean way to implement this?

Define both of them
resources :foobars, path: "foo-bars"
resources :foobars, path: "foobars"
EDIT:
For custom actions instead of declaring them twice for each path like this,
resources :foobars, path: "foo-bars"
collection do
get 'bulk_new'
patch 'bulk_create'
get 'bulk_edit'
patch 'bulk_update'
end
end
resources :foobars, path: "foobars"
collection do
get 'bulk_new'
patch 'bulk_create'
get 'bulk_edit'
patch 'bulk_update'
end
end
Create common block and pass it to both resource method calls.
common_block = lambda do
collection do
get 'bulk_new'
patch 'bulk_create'
get 'bulk_edit'
patch 'bulk_update'
end
end
resources :foobars, path: "foo-bars", &common_block
resources :foobars, path: "foobars", &common_block

Related

rename rails route

I have a route/path using CcpaAcknowledgmentsController
/ccpa_acknowledgments
I would like the route to be, BUT I would still like it to use the CcpaAcknowledgmentsController
/customers/ccpa_acknowledgments
Since I have these two routes...
resources :customers
resources :ccpa_acknowledgments
match '/customers/ccpa_acknowledgments', to: 'ccpa_acknowledgments#index', via: [:get]
I keep getting a conflict stating
NoMethodError in CustomersController.
Is there a way to get the desired route I want without putting the method/code in the CustomersController?
This is the way to do that
resources :customers do
get :ccpa_acknowledgments, to: 'ccpa_acknowledgments#index', on: :collection
end
Inside the customers block for two reasons:
we are fine with the beginning of the path /customers
we don't want to mess with the other customers' routes. In this way your route inside the block is before the customers default routes and it's not seen as you are calling customers/:id with ccpa_acknowledgments as id because rails takes care of that for you defining that route before the show
Then
get :ccpa_acknowledgments
because we need the second part of the path /ccpa_acknowledgments
to: 'ccpa_acknowledgments#index'
we want to specify the controller and action pair, because we want to use the CcpaAcknowledgmentsController even though we're inside the customers block
on: :collection
because we don't want any :id inside our route. It's a route defined on the customers collection
alternative using resources as asked in the comment. Try
scope :customers do
resources :ccpa_acknowledgments, only: :index
end
but you need to put this before the resources :customers

Change name of scaffold routes

Is there a way to change the name of the routes that my scaffold created? I made a scaffold for Cars. Currently I have resources :cars in my routes. How can I change the routes such that my url shows http://localhost:3000/transportation instead of http://localhost:3000/cars? I do not need to change the name of the entity in my schema, all I want to change are the routes associated with it. How can I go about this?
Is there no other way to achieve this but to do a get for each? Ex:
get '/transportation', to: 'cars#index', as: 'cars_index'
You can define the new route after the resources created by your scaffold to respond to your cars controller and index action, or any other other, depending on what you want to achieve.
resources :cars
get 'transportation', to: 'cars#index'
If you want to apply it for all your routes on the car scaffold, then you can pass a path option:
resources :cars, path: 'transportations'
This way the routes pointing to car won't be available and will be replaced for transportations.
You can redefine resource routes with custom URLs by passing a string of your choice along with :path option along with its route definition in routes.rb
resources :cars, :path => "transportation"
With this route definition, access to cars resources in your app will be routed to these URLs
cars GET /transportation(.:format) cars#index
POST /transportation(.:format) cars#create
new_car GET /transportation/new(.:format) cars#new
edit_car GET /transportation/:id/edit(.:format) cars#edit
car GET /transportation/:id(.:format) cars#show
PATCH /transportation/:id(.:format) cars#update
PUT /transportation/:id(.:format) cars#update
DELETE /transportation/:id(.:format) cars#destroy

Can we rename a resource collection/member URL?

I have this resource in routes.rb
resources :orders, only:[] do
collection do
post :user_order_confirmation
end
end
which creates a path like this in HTML views 'orders/carrier_order_confirmation'. Can we do something that this path looks like this 'orders/user_confirmation'?
You can do
resources :orders, only:[] do
collection do
post :user_confirmation, acion: :user_order_confirmation
end
end
This will give you the exact route as you are looking for. Here is the test :
$ rake routes | grep orders
user_confirmation_orders POST /orders/user_confirmation(.:format) orders#user_confirmation {:acion=>:user_order_confirmation}
You can overwrite the conventional routes by adding the following line in your routes.rb after you set the resource routes:
get "/orders/user_confirmation" => "orders#user_confirmation", as: "user_confirmation"
To clarify, here's what each part of that means:
[Routing Method] "/[Route You want]" => "[Controller]#[Controller Method]", as: "[Custom Route Name]"
Try this
post :user_order_confirmation, as: :user_confirmation

Params for namespaced resources (Rails)

Can anyone point me to info on how the name of the params object is generated?
I just spent far too long trying to access params[:venue_search] following a form submission, only to find I needed to be referring instead to params[:searches_venue_search] presumably because I've organised the venue_search resource into a "search" folder and/or because of the preceding "search" directory in the URL that I've specified in the routes.
What's the logic behind this, anyone?
The routes:
resources :venue_searches, controller: 'searches/venue_searches', model: 'searches/venue_search', only: [:create, :new], path: "/search/venues"
match "search/venues/show", to: "searches/venue_searches#show", as: :venue_search
Cheers!
get "search/venues/show", to: "search/venue_searches#show", as: :venue_search
This will generate following route
venue_search GET /search/venues/show(.:format) search/venue_searches#show

Rails routes: collection routes keep adding "_index" to url helper?

I'm trying to create extra paths for my checkout controller. Here are my routes:
resources :checkout do
collection do
post :add_to_cart, :to => 'checkout#add_to_cart'
put :update_shopping_cart, :to => 'checkout#update_shopping_cart'
get :billing
post :update_billing
put :update_billing
post :update_shipping
put :update_shipping
get :order_summary
post :submit_order
get :order_complete
get :clone_shipping_address
get :estimate_shipping
end
end
However, when I do a bundle exec rake routes | grep checkout, all my custom routes have an _index suffix that I don't want:
add_to_cart_checkout_index POST /checkout/add_to_cart(.:format) checkout#add_to_cart
update_shopping_cart_checkout_index PUT /checkout/update_shopping_cart(.:format) checkout#update_shopping_cart
billing_checkout_index GET /checkout/billing(.:format) checkout#billing
update_billing_checkout_index POST /checkout/update_billing(.:format) checkout#update_billing
PUT /checkout/update_billing(.:format) checkout#update_billing
update_shipping_checkout_index POST /checkout/update_shipping(.:format) checkout#update_shipping
PUT /checkout/update_shipping(.:format) checkout#update_shipping
order_summary_checkout_index GET /checkout/order_summary(.:format) checkout#order_summary
submit_order_checkout_index POST /checkout/submit_order(.:format) checkout#submit_order
order_complete_checkout_index GET /checkout/order_complete(.:format) checkout#order_complete
clone_shipping_address_checkout_index GET /checkout/clone_shipping_address(.:format) checkout#clone_shipping_address
estimate_shipping_checkout_index GET /checkout/estimate_shipping(.:format) checkout#estimate_shipping
checkout_index GET /checkout(.:format) checkout#index
POST /checkout(.:format) checkout#create
new_checkout GET /checkout/new(.:format) checkout#new
edit_checkout GET /checkout/:id/edit(.:format) checkout#edit
checkout GET /checkout/:id(.:format) checkout#show
PUT /checkout/:id(.:format) checkout#update
DELETE /checkout/:id(.:format) checkout#destroy
How would I remove that?
You have to pluralize the name of the resources:
resources :checkouts do
## your routes
end
In fact you have to use resource :checkout or resources :checkouts, depending on what you need.

Resources