ruby-on-rails - rake 路由上未初始化的常量 API

标签 ruby-on-rails ruby api

正在关注 this tutorial ,我在 Rails 上创建了我现有博客 Web 应用程序的 API。我收到错误:

uninitialized constant API

这是我的代码:

lib/api/v1/articles.rb:

module API
 module V1
   class Articles < Grape::API
  version 'v1'
  format :json

  resource :articles do
    desc "Return list of recent posts"
    get do
     Article.recent.all
    end
  end
 end
end
end

lib/api/v1/root/rb

module API
 module V1
 class Root < Grape::API
  mount API::V1::Articles
     end
 end
end

lib/api/root.rb

module API
class Root < Grape::API
    prefix 'api'
    mount API::V1::Root
    end
end

lib/tasks/routes.rake

namespace :api do
  desc "API Routes"
  task :routes => :environment do
   API::Root.routes.each do |api|
  method = api.route_method.ljust(10)
  path = api.route_path.gsub(":version", api.route_version)
  puts "     #{method} #{path}"
 end
end
end

config/routes.rb

 Rails.application.routes.draw do
  mount API::Root => '/'
 get 'welcome/index'
 root 'welcome#index'
  resources :articles
  end

这是现有的网络应用程序代码:

app/controllers/articles_controller.rb

class ArticlesController < ApplicationController
def new
end
def create
   # render plain: params[:article].inspect
     @article = Article.new(article_params)

 @article.save
 redirect_to @article
    end
def show
 @article = Article.find(params[:id])
end
private

def article_params
    params.require(:article).permit(:title,:text)
    end

end

现在我处于本文的访问 API 路由部分。当我运行 rake routes 时,它给出了错误,未初始化的常量 API。我做错了什么。

编辑:根据评论,给出详细错误

rake aborted!
NameError: Uninitialized constant API
F:/blog/config/routes.rb:2:in 'block in <top (required)>'
F:/blog/config/routes.rb:1:in '<top (required)>'
C:in 'execute_if_updated'
F:/blog/config/environment.rb:5:in '<top (required)>'
Tasks: TOP =>routes =>environment

environment.rb 的内容

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
Rails.application.initialize!

当我在你的 config/application.rb 中插入 config.autoload_paths += Dir["#{config.root}/lib/**/"] 并运行 rake routes 时,它给出错误 can' t 将 Symbol 转换为 String

enter image description here

升级葡萄版本,报错:

enter image description here

最佳答案

根据讨论,您需要将 grape gem 的版本更新为 0.9.0,然后您需要在 Gemfile 中添加以下行:

gem 'grape', '0.9.0'

然后:

$ bundle install

关于ruby-on-rails - rake 路由上未初始化的常量 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26335006/

相关文章:

ruby-on-rails - ruby rails : RVM and Rubygems Error

ruby - 如何在同一个 ssh session 中执行 2 个或更多命令?

python - TWS API(盈透证券)-如何捕获事件并下订单

html - 将 HTML 转换为 markdown

ruby-on-rails - ruby 西纳特拉 : redirect page not working?

ruby-on-rails - 如何在 Rails 4 中使用 page.replace_html

javascript - 将 JavaScript 转换为 Coffee?

ruby - 使用 rspec 测试成功返回的线程数

api - REST API : Simple token-based authentication - security?

api - 通过msxml2.ServerXMLHTTP提供身份验证信息