Rails Routes - Limiting the available formats for a resource

后端 未结 6 1906
孤城傲影
孤城傲影 2020-11-30 08:31

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

6条回答
  •  无人及你
    2020-11-30 09:15

    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

提交回复
热议问题