2015-12-28 18 views
2

我有一个由门卫保护的葡萄API,我有一堆完美工作的方法。但是,有一种方法行为异常。 它是一种不需要参数和运行它引发以下错误GET请求:Rails葡萄API的ID无效请求中无需ID

Grape::Exceptions::ValidationErrors at /v1/discount_cards/all.json

id is invalid

我的方法是这样的:

desc 'Get all the cards of current customer' 
params {} 
get 'all' do 
    current_customer.discount_cards.to_json(include: 
    { 
    barcode: { 
     include: :barcode_type 
    } 
    }) 
end 

日志说,错误在该行17发生logger.rb文件,它看起来像这样:

module API 
    class Logger 
    def initialize(app) 
     @app = app 
    end 

    def call(env) 
     payload = { 
     remote_addr: env['REMOTE_ADDR'], 
     request_method: env['REQUEST_METHOD'], 
     request_path: env['PATH_INFO'], 
     request_query: env['QUERY_STRING'], 
     x_organization: env['HTTP_X_ORGANIZATION'] 
     } 

     ActiveSupport::Notifications.instrument 'grape.request', payload do 
     @app.call(env).tap do |response| # <-- logs say the error is here 
      payload[:params] = env['api.endpoint'].params.to_hash 
      payload[:params].delete('route_info') 
      payload[:params].delete('format') 
      payload[:response_status] = response[0] 
     end 
     end 
    end 
    end 
end 

我的主要base.rb类看起来是这样的:

module API 
    class Dispatch < Grape::API 
    mount API::V1::Base 
    end 

    Base = Rack::Builder.new do 
    use API::Logger 
    run API::Dispatch 
    end 
end 

我实在无法理解什么id它在谈论,此外,其他所有的API方法的工作精绝(POST,GET,PUT,删除)。

你能帮我解决这个问题吗?

+0

您是否找到解决方案? 即使我面临同样的问题,我无法理解它要从错误消息中传达的内容。 –

回答

2

我最近也遇到了这个问题,而且事实证明,这是我的代码顺序问题。我找到了答案here

Grape evaluates routes in order, so with two routes, /:id and /nearby_missions, it matches /:id first, making id=nearby_missions. Then it tries to coerce nearby_missions into an Integer which fails, and you get the missing id error.

You could "fix" this by adding requirements: /\d*/ to the first route (didn't test), but you're probably just better off ordering them in the order you want them to be evaluated, which is what you did.

有没有在你的问题足够的信息,我可以肯定的,这也是你的问题,但它很可能是!