【问题标题】:NameError in ProjectsController#indexProjectsController#index 中的名称错误
【发布时间】:2016-10-29 13:47:48
【问题描述】:

在我的 Rails 应用程序中,我收到以下错误:

NameError in ProjectsController#index
app/controllers/projects_controller.rb:6:in `index'

我的路线如下所示:

RailsStarter::Application.routes.draw do
resources :projects

root :to => 'pages#home'
match '/' => 'pages#home'
match '/overview' => 'pages#overview'
match '/recruitmentInfo' => 'pages#recruitmentInfo'

问题控制者:

 class ProjectsController < ApplicationController

 # GET /projects
 # GET /projects.json
 def index
 @projects = ::Project.all

 respond_to do |format|
   format.html # index.html.erb
   format.json { render json: @projects }
 end
end

到目前为止,我的调查表明 Project 尚未初始化,但这无关紧要,因为它是一个构造函数,在它自己的类中。我什至尝试通过将这一行 config.autoload_paths += %W( #{config.root}/app/controllers) 添加到 application.rb 来预编译它。似乎还没有任何效果。任何想法将不胜感激。谢谢!

【问题讨论】:

  • 你为什么不选择@projects = Project.all?你真的不需要命名空间解析操作符吗?
  • 除了抛出错误uninitialized constant ProjectsController::Project之外没有任何区别
  • 嗯,这意味着没有定义Project类。您是否生成了 Project 模型?
  • 不,因为我使用了脚手架,我希望也能为我生成模型
  • 当我执行rails generate model Project id:integer primary_key name:string 时,命令返回 0,没有错误,但这不会生成我的模型

标签: ruby-on-rails ruby-on-rails-3 controller constant-expression


【解决方案1】:

[已解决]:

1 Reboot machine - the command must interfere with another server process
2 Check active_support gem versions by running `gem list` and remove them all
3 Remove `Gemfile.lock`
3 Run `bundle install` - this will add back your new version of gemfile
4 Run `rails generate model Project` again

现在应该修复了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2014-09-18
    • 2011-09-23
    • 1970-01-01
    相关资源
    最近更新 更多