【问题标题】:Rails Routes - Limiting the available formats for a resourceRails Routes - 限制资源的可用格式
【发布时间】:2011-04-10 09:38:48
【问题描述】:

我有一系列资源,我只希望通过 JS 格式访问这些资源。 Rails 的路线资源为我提供了格式和标准 HTML。有没有办法指定只创建 JS 格式的路由?

【问题讨论】:

  • 你使用的是什么版本的 Rails?
  • 你能接受我下面的答案吗,它是正确的,当前选择的答案是错误的,让社区感到困惑。

标签: ruby-on-rails routing routes


【解决方案1】:

您必须将这些路由包装在一个范围内。不幸的是,在这种情况下,约束不能按预期工作。

这是一个这样的块的例子......

scope :format => true, :constraints => { :format => 'json' } do
  get '/bar' => "bar#index_with_json"
end

更多信息可以在这里找到:https://github.com/rails/rails/issues/5548

【讨论】:

  • 如果您使用resources,则不需要范围块,只需将:format => true:constraints => ... 直接添加到resources 调用中即可。
  • 这在我的情况下适用于资源丰富的路线。 resources :photos, format: true, constraints: 'json'
  • 不幸的是,这似乎需要 url 上有文件扩展名
  • @steve.hanson 为避免 URL 中的格式要求,使用 lambda 进行约束:get :foo, constraints: lambda { |req| req.format == :json }
  • 问题是当我将路由包装在一个范围块中时,我的 rails 中的其他代码失败了,因为 link_to 命令再也找不到这个包装的路由了
【解决方案2】:

您只需添加有关格式的约束:

resources :photos, :constraints => {:format => /(js|json)/}

【讨论】:

  • 除非我做错了什么,否则我仍然可以以 :html 的形式访问 /photos。当我期望丢失路由异常时,我收到了丢失的模板消息。想法?
  • 不应该是/(js|json)/吗?
  • 是的,我发现并更改了它。仍然对我不起作用。我有资源 :members, :controller => 'homes/members', :constraints => {:format => /js/}
  • 这不会限制对这些格式的请求,请参阅下面的答案以了解正确的实现
【解决方案3】:

以上解决方案都不适合我。我最终选择了这个解决方案:

post "/test/suggestions", to: "test#suggestions", :constraints => -> (req) { req.xhr? }

发现于https://railsadventures.wordpress.com/2012/10/07/routing-only-ajax-requests-in-ror/#comment-375

【讨论】:

    【解决方案4】:

    怎么样

    # routes.rb
    
    class OnlyAjaxRequest
      def matches?(request)
        request.xhr?
      end
    end
    
    post "/test/suggestions", to: "test#suggestions", :constraints => OnlyAjaxRequest.new
    

    它根本没有到达控制器。取自railsadventures

    【讨论】:

      【解决方案5】:

      如果您不仅需要json 之外的一个或另一个(不能使用#xhr?),我在下面为您提供选项

      resource :offers, only: :show, format: true, constraints: { format: 'pdf' }

      希望对你有帮助

      【讨论】:

        【解决方案6】:

        我就是这样做的:

        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
        

        如果你只关心格式,只留下 request.format 部分

        【讨论】:

          【解决方案7】:

          除非请求格式为MIME::JS,否则您可以使用引发路由错误的before_filter

          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
          

          使用:only:except:skip_before_filter 更方便地应用此过滤器,如轨道Action Controller Guide 中所涵盖的那样

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-25
          • 1970-01-01
          • 1970-01-01
          • 2012-11-24
          • 2011-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多