Rails 3 - Restricting Formats for Action in Resource Routes

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



Leave a reply



Submit