Rails Routes - Limiting the available formats for a resource

Viewed 18613

I have a series of resources that I want only available if accessed via the JS format. Rails' route resources gives me the formats plus the standard HTML. Is there a way to specify that only the JS format routes be created?

7 Answers

That's how I do it:

class OnlyAjaxRequest  
    def matches?(request)
      request.xhr? and request.format.to_s.match(/(js|json|javascript)/).present?
    end
  end

match 'remote_login', to: 'remote_content#remote_login', via: [:get], :constraints => OnlyAjaxRequest.new

If you only care about the format, leave just the request.format part

Related