Here is my route:
resources :campaigns, only: [:index, :show]
get '/signs/:sign_id', to: 'signs#show', as: 'sign'
#...others like this...
I'm wanting to append a sub-route to the end of each of the routes above. The sub-route I want to append is:
'/location/:location_id'
This way, I would be able to access:
/campaigns/1
/campaigns/1/location/2
/signs/13
/signs/1/location/12
etc.
I looked into Routing Concerns, but I'm not sure if that would solve my problem. I tried something like this:
#routes.rb
concern :locationable do
member do
get '/location/:location_id'
end
end
resources :campaigns, only: [:show], concerns: :locationable
But obviously this is wrong and it does not work (does not add anything to rake routes). How can I achieve a dry routing solution?
Define the location route as a resource under the concern, like this:
concern :locationable do
resources :locations, only: :show
end
resources :campaigns, only: :show, concerns: :locationable
resources :signs, only: :show, concerns: :locationable
That will generate the following routes:
$ rake routes
Prefix Verb URI Pattern Controller#Action
campaign_location GET /campaigns/:campaign_id/locations/:id(.:format) locations#show
campaign GET /campaigns/:id(.:format) campaigns#show
sign_location GET /signs/:sign_id/locations/:id(.:format) locations#show
sign GET /signs/:id(.:format) signs#show
Source: http://guides.rubyonrails.org/routing.html#routing-concerns
Related
im currently trying to use the resource but one problem im having , that when i do the following
resource :orders
the route /orders dosent route to OrdersController#index rather it points to the show action of the controller, how can i fix this issue ?
becuase of this problem im having to do this which i feel is kinda hack and not good
get '/orders', to: 'orders#index'
get '/orders/:id', to: 'orders#show'
this is my routes.rb file
Rails.application.routes.draw do
get '/carts', to: 'carts#index'
get '/payments', to: 'payments#index'
post '/payments', to: 'payments#add_credits'
get '/orders', to: 'orders#index'
get '/orders/:id', to: 'orders#show'
resources :users do
resource :orders, only: %i[show create index]
resource :carts, only: %i[create destroy], path: 'cart', as: 'cart'
end
resource :sessions, only: [] do
post 'login', action: :create
post 'logout', action: :destroy
get 'login', action: :new
end
resources :products
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
thanks for your answer :)
Don't use
resource :orders
use
resources :orders
You would only use resource when the item orders is a single entity in your application... which is to say you're using the plural to refer to that one item.
Move the resource for orders outside of the Users routes.
Just and FYI, you can have both the full resource outside of Users and then those restricted routes inside Users, but I'm not sure what the goal is here so it is up to you to decide that.
Rails.application.routes.draw do
get '/carts', to: 'carts#index'
get '/payments', to: 'payments#index'
post '/payments', to: 'payments#add_credits'
resources :orders
resources :users do
resource :orders, only: %i[show create index] <-- not sure if this remains here
resource :carts, only: %i[create destroy], path: 'cart', as: 'cart'
end
...
So I have some API resources in my app, and some regular resources, and for regular resources I use :
resources :books
And then I could pass except: %i(destroy new edit) or only so works great! However for my resource I ll never have new/edit actions, sometimes I will need to pass except and only options too.
I was thinking to create something like:
api_resources :books
Which comes without new/edit actions by default, how would I do that?
Maybe something like this?
# config/routes.rb
Rails.application.routes.draw do
def api_resources(res)
resources res, only: [:new, :edit]
end
api_resources :a
api_resources :b
end
# output
Prefix Verb URI Pattern Controller#Action
new_a GET /a/new(.:format) a#new
edit_a GET /a/:id/edit(.:format) a#edit
new_b GET /b/new(.:format) b#new
edit_b GET /b/:id/edit(.:format) b#edit
#Amree's answer can not handle nested resources. An improvement would be:
# config/routes.rb
Rails.application.routes.draw do
def editable_resoustrong textrces(res, &block)
resources res, only: %i[new edit], &block
end
editable_resources :a
end
# output
Prefix Verb URI Pattern Controller#Action
new_a GET /a/new(.:format) a#new
edit_a GET /a/:id/edit(.:format) a#edit
I my routes file, I have defined a resource
namespace :admin do
resources :invoices, only: [:index, :new]
end
Then I've got a route rule with corresponding path helper new_admin_invoice_path
new_admin_invoice GET /admin/invoices/new(.:format) admin/invoices#new
But how can I add two more new rules, so thуe look like
new_admin_incoming_invoice GET /admin/invoices/new/incoming(.:format) admin/invoices#new {:type=>:incoming}
new_admin_outgoing_invoice GET /admin/invoices/new/outgoing(.:format) admin/invoices#new {:type=>:outgoing}
I tried add them manually
resources :invoices, only: [:index, :new] do
get 'new/incoming', on: :collection, action: :new, type: :incoming
get 'new/outgoing', on: :collection, action: :new, type: :outgoing
end
But got wrong result
new_incoming_admin_invoices GET /admin/invoices/new/incoming(.:format) admin/invoices#new {:type=>:incoming}
new_outgoing_admin_invoices GET /admin/invoices/new/outgoing(.:format) admin/invoices#new {:type=>:outgoing}
How can I get exactly that routes with path helpers what I need?
Here is the easy rails way from official guide
resources :invoices, only: [:index] do
get 'incoming', on: :new, type: :incoming, action: :new
end
Results to
incoming_new_admin_invoice GET /admin/invoices/new/incoming(.:format) admin/invoices#new {:type=>:incoming}
Try by using a scope like this:
scope "/admin" do resources :invoices end
I want to properly add new route to already existing route resources in Spree.
Desired end url format: /orders/:order_id/order_returns(.:format)
I can achieve this by adding following snippet to routes.rb:
Rails.application.routes.draw do
resources :orders, only: [] do
resources :order_returns, controller: 'order_returns', only: [:create]
end
end
but resources :orders, only: [] do looks kind of ugly with empty hash and if I remove it Rails will generate routes for orders :(
Is there a better, Rails/Spree way to achieve that ?
I understand resource and path routes in rails 3 but I do not know is there any way to have both routes ? I try this routes but it not work for me, this is the routes:
resources :roles, only: [:index, :create, :show, :update]
get '/roles/:id' => 'roles#available_users'
How can we routes to use both routes ?
thankyou very much
Routes
What you're asking for cannot be done, as you'll be using the same "route" for different controller actions:
#config/routes.rb
resources :roles, only: [:index, :create, :show, :update] #-> domain.com/roles/:id - roles#show
If you then create another route for domain.com/roles/:id, Rails will just take the first which it finds in the routes file
--
The way to fix your issue is likely to be down to the following:
#config/routes.rb
resources :roles, except: [:edit, :destroy] do
get :available_users # -> domain.com/roles/:id/available_users
end
This will take you to the roles#available_users action, providing you with the ability to load the view you wish (to display the users for a particular role)
For a more defined explanation, I'd recommend checking out the nested_resources part of the Rails routing system
If I understand you correctly you want something like this:
resources :roles, only: [:index, :create, :update] do
get '/roles/:id' => 'roles#available_users'
end
Correct?
Just add an "do" behind the closing ] and an end after the custom routes.
Edit: Apparently I got wrong. ;) What you could do is:
resources :roles, only: [:index, :create, :show, :update] do
get '/roles/:id/available' => 'roles#available_users'
end