I have the following route:
resources :trees, shallow: true, controller: 'base', param: :ed_node_id, only: %i[index show] do
resources :courses, shallow: true, controller: 'base', param: :ed_node_id, only: %i[index show]
end
Which gives me:
api_v1_tree_courses GET /api/v1/trees/:tree_ed_node_id/courses(.:format)
api_v1_course GET /api/v1/courses/:ed_node_id(.:format)
api_v1_trees GET /api/v1/trees(.:format)
api_v1_tree GET /api/v1/trees/:ed_node_id(.:format)
What i’m trying to avoid is on that first route, the param not being prefixed by :tree. Is there a way in nested routes to force the param not to have a parent route prefix or possibly using a regex to give the desired route?
Unfortunately Rails just generates the param key for nested resources by combining the singular form of the name with the param option:
module ActionDispatch
module Routing
module Mapper
class Resource
...
def nested_param
:"#{singular}_#{param}"
end
end
end
end
end
There is no option to actually alter the entire nested param - it would make a good feature request though.
The alternative is to use scope:
resources :trees, only: :index
scope '/trees/:ed_node_id' do
resources :courses,
only: :index,
as: :tree_courses
end
# shallow nesting won't work so we have to define this separately.
resources :courses, only: [:show]
Related
Because it's been a while since I was using Rails monolith instead of GrapeAPI I've silly question. I want to create a route for path - users/portfolios/1/portfolio_reports/archived_reports where I will displays PortfolioReports.where(status: 'archived'). I created routes:
namespace :users do
resources :portfolios, only: [:index, :show] do
resources :archived_report, only: [:index, :show]
resources :portfolio_report, only: [:index, :show]
end
end
So I've got two questions: Should the routes file look like my current routes.rb ? and if I have Portfolio and PortfolioReport models like below, the portfolio_reports_controller should be inside app/controllers/users/portfolio_reports_controller.rb or app/controllers/portfolio_reports_controller.rb ?
class Portfolio
has_many :portfolio_reports
end
class PortfolioReport
belongs_to :portfolio
end
In Rails you can use "Shallow Nesting" which basically says to only nest the index, new and create actions under the parent resource. For the other actions you don't need to nest the routes, because through the record itself you have access to the associated record, so there is no need to have the id in the url.
So your routes will be:
users/portfolios/ # Portfolios#index
users/portfolios/1 # Portfolios#show
users/portfolios/1/portfolio_reports # PortfolioReports#index
users/portfolio_reports/1 # PortfolioReports#show
users/portfolios/1/archived_reports # ArchivedReports#index
users/archived_reports/1 # ArchivedReports#show
And routes.rb should look like this:
namespace :users do
resources :portfolios, only: [:index, :show] do
resources :archived_report, only: [:index]
resources :portfolio_report, only: [:index]
end
resources :archived_report, only: [:show]
resources :portfolio_report, only: [:show]
end
(If you'd use all 7 routes you could use the helper shallow as mentioned in the docs).
No need to nest archived_reports under portfolio_reports like you mentioned in your question!
Find more info on shallow nesting here: https://guides.rubyonrails.org/routing.html#shallow-nesting
For the user namespace:
Your controllers should live in a subfolder user because you have the namespace user:
app/controllers/user/portfolio_reports_controller.rb
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 ?
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
I have the following router:
scope ':name' do # Category :name
get :animators, controller: 'categories'
get :creators, controller: 'categories'
resources :items, only: [:show]
end
it generates the following URLs:
http://localhost:3000/birthday/ # index page
http://localhost:3000/birthday/item/123 # resource show page
However what I would like to do is make my second URL look like this
http://localhost:3000/birthday/animator/123 # resource show page
On my Item model Animator is a :type
If I do it with inner scope
scope ':type' do
resources :items, only: [:show]
end
I will get
http://localhost:3000/birthday/animator/item/123
But I would like to get rid of item, plus it makes me indicate an additional parameter when using link_to in the view, which is not good.
scope ':name' do
get :animators, controller: 'categories'
get :creators, controller: 'categories'
resources :items, only: [:show]
get '/animator/:id' => 'animator#show'
end
resources :items, only: [:show] will only catch routes like /birthday/items/1, /birthday/items/2 etc. While get '/animator/:id' => 'animator#show will be able to catch the routes like you have mentioned in your question:
http://localhost:3000/birthday/animator/123
I've added
get ':type/:slug', to: 'items#show', as: :item
to the scope block. It doesn't look pretty IMO, but I will try to fix that eventually.
The two particular routes im having issues with are admin/inspections and admin/activities. When I first save routes.rb, whichever route I load first works but the other will not, it gives me error: "Unitialized constant Admin::Towers"
I have the following routes setup.
namespace :admin do
#...etc...
resources :inspections, only: [:index,:show], controller: 'towers/inspections'
resources :activities, only: [:index], controller: 'towers/activities'
end
As you've namespaced your resources, your controllers should reside within app/controllers/admin/* and have a name, i.e. for inspections: class Admin::InspectionsController
I'm guessing you would like to have:
scope '/admin' do
resources :inspections, only: [:index,:show], controller: 'towers/inspections'
resources :activities, only: [:index], controller: 'towers/activities'
end