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?
-
1What version of Rails are you using?– GarrettSep 9, 2010 at 20:56
-
2Can you accept my answer below, its the correct one, the current selected answer is wrong and confusing the community.– koonseMay 1, 2013 at 17:03
7 Answers
You must wrap those routes in a scope. Constraints unfortunately don't work as expected in this case.
This is an example of such a block...
scope :format => true, :constraints => { :format => 'json' } do
get '/bar' => "bar#index_with_json"
end
More information can be found here: https://github.com/rails/rails/issues/5548
-
4If you're using
resources
, you don't need a scope block, just add the:format => true
and:constraints => ...
directly to theresources
call.– NathanNov 26, 2014 at 0:11 -
This worked in my case for resourcefull route.
resources :photos, format: true, constraints: 'json'
– maicherDec 24, 2014 at 11:41 -
2Unfortunately, it seems that this requires the url to have the file extension on it Jan 31, 2015 at 6:55
-
1@steve.hanson to avoid the format requirement in the URL, use a lambda for constraint:
get :foo, constraints: lambda { |req| req.format == :json }
.– RocketRMay 17, 2017 at 12:00 -
The problem is when I wrap the route in a scope block, other code in my rails fails because the link_to command cant find this wrapped route anymore– Nathan BJul 21, 2021 at 7:06
You just add constraints about format :
resources :photos, :constraints => {:format => /(js|json)/}
-
Unless I'm doing something wrong, that still allows me to access /photos as :html. I get the missing template message, when I'd expect a missing route exception. Thoughts?– Eric M.Sep 9, 2010 at 20:51
-
-
Yeah, I caught that and changed it. Still doesn't work for me. I have resources :members, :controller => 'homes/members', :constraints => {:format => /js/}– Eric M.Sep 9, 2010 at 21:29
-
4this will not limit requests to those formats, see my answer below for the correct implementation– koonseFeb 6, 2013 at 7:32
None of the above solutions worked for me. I ended up going with this solution:
post "/test/suggestions", to: "test#suggestions", :constraints => -> (req) { req.xhr? }
Found on https://railsadventures.wordpress.com/2012/10/07/routing-only-ajax-requests-in-ror/#comment-375
how about
# routes.rb
class OnlyAjaxRequest
def matches?(request)
request.xhr?
end
end
post "/test/suggestions", to: "test#suggestions", :constraints => OnlyAjaxRequest.new
it doesn't get to the controller at all. Taken from railsadventures
If you need not only one or another than json
(cant use #xhr?
) I offer to you option below
resource :offers, only: :show, format: true, constraints: { format: 'pdf' }
Hope it helps
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
You can use a before_filter
that raises a routing error unless the request format is MIME::JS
.
app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base
before_filter :check_js
private
def check_js
raise RoutingError.new('expected application/json') unless request.format == MIME::JS
end
end
Apply this filter more surgically with :only
, :except
, and :skip_before_filter
as covered in the rails Action Controller Guide
-
I've used a similar approach to handle this kind of problem github.com/marcusg/force_format Sep 29, 2013 at 19:13