【问题标题】:How to build named helper using Grape on rails如何使用 Grape on rails 构建命名助手
【发布时间】:2015-02-19 02:45:47
【问题描述】:

我正在使用葡萄宝石;参考https://github.com/intridea/grape.

你能告诉我如何构建像“twitter_api_v1_statuses_path”这样的命名路径吗?

我的代码如下

module Twitter
  class API < Grape::API
    version 'v1', using: :header, vendor: 'twitter'
    format :json
    prefix :api

    resource :statuses do
      desc "Return a public timeline."
      get :public_timeline do
        Status.limit(20)
      end
    end
end

【问题讨论】:

    标签: ruby-on-rails grape-api url-helper


    【解决方案1】:

    我假设您想要一个像 http://yourdomain.com/api/v1/statuses/public_timeline 这样的 URL。在这种情况下,您的 API 类中只有一个问题,它与您选择的版本控制策略有关。 :header 策略在特定标头中搜索 API 版本,这不是您要查找的内容。将其更改为 :path

    module Twitter
        class API < Grape::API
            version 'v1', using: :path, vendor: 'twitter'
            format :json
            prefix :api
    
            resource :statuses do
                desc "Return a public timeline."
                get :public_timeline do
                    Status.limit(20)
                end
            end
        end
    end
    

    【讨论】:

    • :header 策略已更改为 :path,但我无法在 app/controllers/*** 中获取 API 的命名路径我在 config/routes 中添加了挂载策略,例如 mount Twitter::API =&gt; '/' 我的目的如下在 app/controllers/sample_controller.rb redirect_to twitter_api_v1_statuses_path
    猜你喜欢
    • 2014-03-26
    • 2011-04-21
    • 2015-03-03
    • 2013-12-06
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    相关资源
    最近更新 更多