Rails 3 - Restricting formats for action in resource routes
I found that this seemed to work (thanks to @Pan for pointing me in the right direction):
resources :categories, :except => [:show]
resources :categories, :only => [:show], :defaults => { :format => 'json' }
The above seems to force the router into serving a format-less request, to the show action, as json by default.
Rails Routes - Limiting the available formats for a resource
You just add constraints about format :
resources :photos, :constraints => {:format => /(js|json)/}
Restricting Rails routes to actions
You can exempt the new
route from the users resource, and replace it with your custom route:
resources :users, except: [:new]
get 'signup', to: 'users#new', as: "new_user"
Resulting in:
users GET /users(.:format) users#index
POST /users(.:format) users#create
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
new_user GET /signup(.:format) users#new
Restricting resource routes and adding additional non-RESTful routes in Rails 3
You can pass arguments and a block to resources
:
resources :sessions, :only => [:new, :create, :destroy] do
get :recovery, :on => :member
end
And test it out with rake routes
.
Restricting routes to nested resources in Rails
Is this what you're trying to do?
map.resources :persons do |person|
person.resources :addresses, :only => [:index, :show]
end
This creates addresses as a nested resource of persons, but only makes the index and show views available.
Rails 3 route scope, exclude one action?
I think
scope "/admin", do
resources :profiles, except: :show
end
is what you need.
see more at http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
Rails - good practice to specify the action available for a resource?
I see couple benefits here beside not being a lazy Engineer ;)
DRY routes, clean code
You might have lots of routes in your Rails application at some point. Limiting the REST routes to only the one you need will help you to see what routes are actually available from your application.
When running rake routes
, having only useful routes will be way more efficient that having all bunch of used and unused routes merged together.
Routes are parsed in order
Every time you or someone makes a HTTP request on your server, Rails has to parse your route(s) file(s) to find a match.
Rails routes are matched in the order they are specified, so if you
have a resources :photos above a get 'photos/poll' the show action's
route for the resources line will be matched before the get line.
Having unused routes might slow down Rails parsing at some point (you wont see any differences with a small amount of routes).
Related Topics
Rails with Paypal Permissions and Paypal Express Checkout
Invalid Multibyte Char (Utf-8) Error, Ruby
Could Not Find a Valid Gem 'Rails' (>= 0) in Any Repository
How to Call Java API from Ruby 1.8 or 1.9
How to Authorize a Service Account for Google Calendar API in Ruby
How to Emit Comments in a Yaml Document Using Psych
Ruby Use Array Tvalues to Index Nested Hash of Hash
How to Fetch Linkedin User Data
Ruby Shoes Gui: Continually Updating Paragraphs
Why Does This Nokogiri Xpath Have a Null Return
Mailer Unable to Access Reset_Token in User Model
How to Use Ruby's Readlines.Grep for Utf-16 Files
Tzinfo-Data Present But Not Seen
Having Trouble Installing Any Ruby 1.9.X (With Rbenv) on MAC Osx Due to Psych Yaml Parse Errors
Open_Id_Authentication - "Openidauthentication.Store Is Nil. Using In-Memory Store." Problem
Ruby SASS, Unable to Resolve Dependancies
Get SASS from Database (Compile Passed Data Instead of Reading from File)