Rails routes: Nested, Member, Collection, namespace, scope and customizable
**Collection & Member routes**
A member route requires an ID, because it acts on a member.
A collection route doesn't require an ID because it acts on a
collection of objects
:member
creates path with pattern /:controller/:id/:your_method
:collection
creates path with the pattern /:controller/:your_method
For example :
map.resources :users, :collection => { :abc => :get } => /users/abc
map.resources :users, :member => { :abc => :get } => /users/1/abc
**Scopes & Namespaces routes**
namespace
andscope
in the Railsroutes
affect the controller
names, URIs, and named routes.The scope method gives you fine-grained control:
scope 'url_path_prefix', module: 'module_prefix', as: 'named_route_prefix' do
resources :model_name
end
For Example :
scope 'foo', module: 'bar', as: 'baz' do
resources :posts
end
produces routes as : Prefix Verb URI Pattern Controller#Action
baz_posts GET /foo/posts(.:format) bar/posts#index
POST /foo/posts(.:format) bar/posts#create
new_baz_post GET /foo/posts/new(.:format) bar/posts#new
edit_baz_post GET /foo/posts/:id/edit(.:format) bar/posts#edit
baz_post GET /foo/posts/:id(.:format) bar/posts#show
PATCH /foo/posts/:id(.:format) bar/posts#update
PUT /foo/posts/:id(.:format) bar/posts#update
DELETE /foo/posts/:id(.:format) bar/posts#destroy
The namespace method is the simple case — it prefixes everything.
namespace :foo do
resources :posts
end
produces routes as : Prefix Verb URI Pattern Controller#Action
foo_posts GET /foo/posts(.:format) foo/posts#index
POST /foo/posts(.:format) foo/posts#create
new_foo_post GET /foo/posts/new(.:format) foo/posts#new
edit_foo_post GET /foo/posts/:id/edit(.:format) foo/posts#edit
foo_post GET /foo/posts/:id(.:format) foo/posts#show
PATCH /foo/posts/:id(.:format) foo/posts#update
PUT /foo/posts/:id(.:format) foo/posts#update
DELETE /foo/posts/:id(.:format) foo/posts#destroy
**Constraints & Redirect**
Rails routes are executed sequentially, you can mimic conditional
login in the following manner:
match '/route' => 'controller#action', :constraints => Model.new
match '/route' => 'user#action'
The first line checks whether the conditions of the constraint are met (i.e., if the request is emanating from a Model domain). If the constraint is satisfied, the request is routed to controller#action.We can add constraints to routes for multiple uses like for ip-matching, params matching, restrict format parameter, request-based restrictions etc as :
- ip-matching
=> resources :model, constraints: { ip: /172\.124\.\d+\.\d+/ }
- filtering id params
=> match 'model/:id', to: 'model#show' ,constraints: { id: /\d+/}, via: :get
- restrict format params
=> match 'model/:id', to: 'model#show' ,constraints: { format: 'json' }, via: :get
- request-based constraints
=> get 'admin/', to: 'admin#show', constraints: { subdomain: 'admin' }
Rails route alias for a nested route with specific id
You can use a match
method in your route.
match '/business-meet/registration', to: 'event_participants#new', via: :get, defaults: { event_id: 8 }
Ruby on Rails - Nested Route Collections
I believe the answer is:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users do
collection do
get "sync"
post "register"
get "types/:type_id", action: 'types'
end
end
end
end
end
types
is the action and what you need is the params :type_id
.if you run
rails routes
, you get: Prefix Verb URI Pattern Controller#Action
GET /api/v1/users/types/:type_id(.:format) api/v1/users#types
Now you can go to http://localhost:3000/api/v1/users/types/10
Rails routing: namespace nested in a scope
Try to add this in your application controller:
def default_url_options(options={})
{:locale => I18n.locale}
end
It worked for me, I suggest you to read the sections of the I18n guide where to explain how to set the locale from the URL params.PS: Welcome to StackOverflow ;)
Rails Routes: Nested Scopes/Namespaces
namespace
is what I was looking for:
constraints(AdminDomain) do
scope :module => "admin" do
resources :visitors
namespace :history do
resources :visitors
end
end
end
And I had to define Admin::History::VisitorsController
in app/controllers/admin/history/visitors_controller.rb
difference between collection route and member route in ruby on rails?
A member route will require an ID, because it acts on a member. A collection route doesn't because it acts on a collection of objects. Preview is an example of a member route, because it acts on (and displays) a single object. Search is an example of a collection route, because it acts on (and displays) a collection of objects.
What is the difference between resources and namespace in rails nested routing
Check out the difference using rake routes
.
This definition with a namespace:
namespace :alpha do
resources :posts
end
results in the following routes: Prefix Verb URI Pattern Controller#Action
alpha_posts GET /alpha/posts(.:format) alpha/posts#index
POST /alpha/posts(.:format) alpha/posts#create
new_alpha_post GET /alpha/posts/new(.:format) alpha/posts#new
edit_alpha_post GET /alpha/posts/:id/edit(.:format) alpha/posts#edit
alpha_post GET /alpha/posts/:id(.:format) alpha/posts#show
PATCH /alpha/posts/:id(.:format) alpha/posts#update
PUT /alpha/posts/:id(.:format) alpha/posts#update
DELETE /alpha/posts/:id(.:format) alpha/posts#destroy
As you can see, the only thing that is different from a plain resources
route set is the addition of /alpha
prefix.Now for the two-level resources
routes definition:
resources :alpha do
resources :posts
end
which results in: Prefix Verb URI Pattern Controller#Action
alpha_posts GET /alpha/:alpha_id/posts(.:format) posts#index
POST /alpha/:alpha_id/posts(.:format) posts#create
new_alpha_post GET /alpha/:alpha_id/posts/new(.:format) posts#new
edit_alpha_post GET /alpha/:alpha_id/posts/:id/edit(.:format) posts#edit
alpha_post GET /alpha/:alpha_id/posts/:id(.:format) posts#show
PATCH /alpha/:alpha_id/posts/:id(.:format) posts#update
PUT /alpha/:alpha_id/posts/:id(.:format) posts#update
DELETE /alpha/:alpha_id/posts/:id(.:format) posts#destroy
alpha_index GET /alpha(.:format) alpha#index
POST /alpha(.:format) alpha#create
new_alpha GET /alpha/new(.:format) alpha#new
edit_alpha GET /alpha/:id/edit(.:format) alpha#edit
alpha GET /alpha/:id(.:format) alpha#show
PATCH /alpha/:id(.:format) alpha#update
PUT /alpha/:id(.:format) alpha#update
DELETE /alpha/:id(.:format) alpha#destroy
As you can see, alpha
becomes a top-level resource with all 8 RESTful routes. posts
, in turn, become second-level resource, accessible only through the route to a specific alpha
object.Read more in Rails Routing from the Outside In.
You might also find interesting the scope
option. Read about the difference between scope
and namespace
in Scoping Rails Routes blog post.
Related Topics
How to Handle 404 Errors with Ruby Http::Net
When Is The Enumerator::Yielder#Yield Method Useful
Difference Between Render and Yield in Rails
How to "Break" Out of a Case...While in Ruby
Actionmailer Pass Local Variables to The Erb Template
How to (Massively) Reduce The Number of SQL Queries in Rails App
Monitor Multiple Rails Applications
Error Could Not Find I18N-0.7.0 in Any of The Sources
Escaping Strings for Ruby Sqlite Insert
If I Have a Stripe Token from a Charge, How to Get Its Charge Id
How to Resize Image Only If Width Exceeds with Graphics/Image Magick
Access Rake Task Description from Within Task
What Is Ruby's Stringio Class Really
Adding Two Activerecord::Relation Objects
Generate Models from Existing Tables Using Rails 3
Broken Rails Routes After Implementing Single Table Inheritance