gpt4 book ai didi

ruby-on-rails - UnInitialized Contant Twitter::Grape 使用 grape 示例代码

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:22 24 4
gpt4 key购买 nike

我正在尝试使用葡萄来制作 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

当我运行命令 rackup 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 文件,但仍然看到错误。

最佳答案

您不需要实现 Grape 的 ruby​​gem。

添加行

需要'葡萄'

grape_sample.rb

如果您仍然遇到问题,请确保您已经安装了 grape gem:

gem 安装葡萄

关于ruby-on-rails - UnInitialized Contant Twitter::Grape 使用 grape 示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14568477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com