未初始化的常量API :: V1 :: UserController

时间:2014-11-07 09:02:35

标签: ruby-on-rails ruby web-services rest rails-api

我知道在stackoverflow上已经存在很多关于这个的问题,但是在我的情况下它们都不起作用。

在我的 routes.rb

Exer9::Application.routes.draw do
    namespace :api, defaults: {format: 'json'} do
        namespace :v1 do
            resources :users
        end
    end
  end

exer9 /应用/控制器/ API / V1 / users_controller.rb

module Api
  module v1
     class UsersController < ApplicationController

      # GET /user
      # GET /user.json
      def index
        @users = User.all

        render json: @users
      end

      def new
      end

      def update
      end
      # GET /user/1
      # GET /user/1.json
      def show
        @user = User.find(params[:id])

        render json: @user
      end

      def create
        @user = User.new(params[:user])

        if @user.save
          render json: @user
        else
          render json: @user.errors
        end

      end

      def delete
      end

      def destroy
      end
end
end
end

更新

这是我的ApplicationController文件

class ApplicationController < ActionController::API
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  # protect_from_forgery with: :exception

end

我得到的错误信息是:

superclass mismatch for class UsersController

Extracted source (around line #2):
1
2
3
4
5
6


     class Api::V1::UsersController < ApplicationController

      # GET /user
      # GET /user.json
      def index

Rails.root: /home/steven/Desktop/weekly-exercises/exer9

Application Trace | Framework Trace | Full Trace
app/controllers/api/v1/users_controller.rb:2:in `<top (required)>

&#39;

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:0)

确保您的文件夹结构正确无误:

users_controller.rb

应该找到:

app/controllers/api/v1/users_controller.rb

答案 1 :(得分:-1)

使用较短的语法,例如:

class Api::V1::UsersController < ApplicationController
  # your code goes here
end

另外,重命名类和文件后重新启动rails服务器吗?

答案 2 :(得分:-1)

您可以像这样使用

在routes.rb

namespace :api, defaults: {format: 'json'} do
  scope module: :v1, constraints: ApiConstraints.new(version: 1 , default: true) do
    resources :users
  end
end

在控制器中

class Api::V1::UsersController < ApplicationController
  # Your code here
end