我试图用葡萄来制作Rest API。
为此,我在一个目录中创建了一个config.ru文件:

require 'rubygems'
require 'rack'
require './grape_sample'
run Twitter::API

有一个名为grape_sample.rb的文件:
module Twitter
class API < Grape::API

version 'v1', :using => :header, :vendor => 'twitter'
format :json

helpers do
  def current_user
    @current_user ||= User.authorize!(env)
  end

  def authenticate!
    error!('401 Unauthorized', 401) unless current_user
  end
end

resource :statuses do

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

  desc "Return a personal timeline."
  get :home_timeline do
    authenticate!
    current_user.statuses.limit(20)
  end

  desc "Return a status."
  params do
    requires :id, :type => Integer, :desc => "Status id."
  end
  get ':id' do
    Status.find(params[:id])
  end

  desc "Create a status."
  params do
    requires :status, :type => String, :desc => "Your status."
  end
  post do
    authenticate!
    Status.create!({
      :user => current_user,
      :text => params[:status]
    })
  end

  desc "Update a status."
  params do
    requires :id, :type => String, :desc => "Status ID."
    requires :status, :type => String, :desc => "Your status."
  end
  put ':id' do
    authenticate!
    current_user.statuses.find(params[:id]).update({
      :user => current_user,
      :text => params[:status]
    })
  end

  desc "Delete a status."
  params do
    requires :id, :type => String, :desc => "Status ID."
  end
  delete ':id' do
    authenticate!
    current_user.statuses.find(params[:id]).destroy
  end

end
end
end

当我运行commandrackup config.ru时,将发生以下错误:
/home/ritesh/rails/grape_sample.rb:2:in `<module:Twitter>': uninitialized constant Twitter::Grape (NameError)
    from /home/ritesh/rails/grape_sample.rb:1:in `<top (required)>'
    from /home/ritesh/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /home/ritesh/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /home/ritesh/rails/config.ru:7:in `block in <main>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/builder.rb:55:in `instance_eval'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/builder.rb:55:in `initialize'
    from /home/ritesh/rails/config.ru:in `new'
    from /home/ritesh/rails/config.ru:in `<main>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/builder.rb:49:in `eval'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/builder.rb:49:in `new_from_string'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/builder.rb:40:in `parse_file'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/server.rb:277:in `build_app_and_options_from_config'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/server.rb:199:in `app'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/server.rb:314:in `wrapped_app'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/server.rb:250:in `start'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/server.rb:141:in `start'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/bin/rackup:4:in `<top (required)>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/bin/rackup:19:in `load'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/bin/rackup:19:in `<main>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `eval'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `<main>'

这次,我在config.ru中包含了require文件,但仍然看到错误。

最佳答案

你不需要葡萄的rubygem。
添加行
require 'grape'
grape_sample.rb
如果您仍然有问题,请确保已安装葡萄宝石:
gem install grape

关于ruby-on-rails - 使用葡萄样本代码时未初始化的Contant Twitter::Grape,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14568477/

10-11 04:35