mackshkatz
mackshkatz

Reputation: 881

Restrict all restful routes without repeating for each resource declaration

I am working on a Rails API (Rails 4.2.0.beta4), and in turn since the client will be responsible for generating the new and edit forms, my RESTful controllers will only need 5 actions. Rather than having to do something like this:

resources :media, except: [:new, :edit]
resources :media_collections, except: [:new, :edit]
etc...

Is there some way I could define all of my resources inside a block that has except: [:new, :edit] stated in one spot or something? Seems crazy to have to append that to ever resource declaration, right?

Upvotes: 0

Views: 92

Answers (1)

Slicedpan
Slicedpan

Reputation: 5015

You can create a method that abstracts this. I would create a new module for this, and use extend to make the methods available in routes.rb

module ApiResource
  def api_resources(name, options = {}, &block)
    resources name, options.merge({:except => [:new, :edit]}, &block)
  end
end

# in routes.rb:

MyApp::Application.routes.draw do
  extend ApiResource

  api_resources :media
  api_resources :media_collections

  # ...

end

Upvotes: 1

Related Questions