NoMethodError:routes_with_grape中的未定义方法

时间:2015-07-01 07:51:19

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

我正在尝试使用以下结构设置rails葡萄api。

app
   api
     api
       V1
         user.rb
       app.rb

当我运行routes_with_grape

时,我收到此错误
rake aborted!
NoMethodError: undefined method `ast' for "/api/ping(.json)":String
/usr/local/rvm/gems/ruby-1.9.3-p551/gems/actionpack-4.2.1/lib/action_dispatch/journey/path/pattern.rb:14:in `initialize'
/usr/local/rvm/gems/ruby-1.9.3-p551/gems/grape-rails-routes-1.0/lib/rails/tasks/routes_with_grape.rake:9:in `new'
/usr/local/rvm/gems/ruby-1.9.3-p551/gems/grape-rails-routes-1.0/lib/rails/tasks/routes_with_grape.rake:9:in `block (3 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p551/gems/grape-rails-routes-1.0/lib/rails/tasks/routes_with_grape.rake:8:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p551/gems/grape-rails-routes-1.0/lib/rails/tasks/routes_with_grape.rake:8:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p551/gems/grape-rails-routes-1.0/lib/rails/tasks/routes_with_grape.rake:6:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p551/gems/grape-rails-routes-1.0/lib/rails/tasks/routes_with_grape.rake:6:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p551/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-1.9.3-p551/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => routes_with_grape
(See full trace by running task with --trace)

这是我的代码

user.rb

module V1
  class User < Grape::API
    desc 'Returns pong.'
    get :ping do
      { ping: params[:pong] || 'pong' }
    end
  end
end

app.rb

class API < Grape::API
  prefix 'api'
  format :json
  mount ::V1::User
end

谢谢!

1 个答案:

答案 0 :(得分:0)

抛弃该gem并使用此代码(从here复制):

sc.getRDDStorageInfo

// Array[org.apache.spark.storage.RDDInfo] = Array(
//   RDD "MapPartitionsRDD" (17) StorageLevel:
//       StorageLevel(false, true, false, false, 1);
//     CachedPartitions: 2000; TotalPartitions: 2000; MemorySize: 8.6 KB; 
//     ExternalBlockStoreSize: 0.0 B; DiskSize: 0.0 B,
//   RDD "MapPartitionsRDD" (7) StorageLevel:
//      StorageLevel(false, true, false, false, 1);
//    CachedPartitions: 8; TotalPartitions: 8; MemorySize: 668.0 B; 
//    ExternalBlockStoreSize: 0.0 B; DiskSize: 0.0 B)

将其粘贴到Rakefile中并修改:

namespace :grape do
  desc "Grape API Routes"
  task :routes => :environment do
    mapped_prefix = '/api' # where mounted API in routes.rb
    params_str = ' params:'
    desc_limit = 45
    route_info = API.routes.map {|r| [r, r.instance_variable_get(:@options)] }
    max_desc_size     = route_info.map{|_,info| (info[:description] || '')[0..desc_limit].size }.max
    max_method_size   = route_info.map{|_,info| info[:method].size }.max
    max_version_size  = route_info.map{|_,info| info[:version].size }.max
    max_path_size     = route_info.map{|_,info| info[:path].sub(':version', info[:version]).size }.max
    max_params_digits = route_info.map{|_,info| info[:params].size.to_s.size }.max
    format_str = format(
      '%%%ds  %%%ds %%%ds %%%ds%%-%ds | %%%ds%%%ds  %%s',
      max_desc_size + 1,
      max_version_size,
      max_method_size,
      mapped_prefix.size,
      max_path_size,
      max_params_digits,
      params_str.size)

    route_info.each do |_,info|
      fields = [
        info[:description] ? info[:description][0..desc_limit] : '',
        info[:version],
        info[:method],
        mapped_prefix,
        info[:path].sub(':version', info[:version]),
        info[:params].size.to_s,
        params_str,
        info[:params].first.inspect,
      ]
      puts format(format_str, *fields)

      info[:params].drop(1).each do |param|
        puts format(format_str, *([''] * (fields.size-1)) + [param.inspect])
      end
    end
  end
end

在我的情况下使用您的API安装点: 在app / api / web / api.rb下我有

route_info = API.routes.map

所以我用Web :: API.routes替换了API.routes