Ruby-on-rails 未初始化的常量 HomeController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4456967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
uninitialized constant HomeController
提问by onaclov2000
ok, I've been following: http://railscasts.com/episodes/196-nested-model-form-part-1
好的,我一直在关注:http: //railscasts.com/episodes/196-nested-model-form-part-1
Here are the steps I've had to accomplish so far:
以下是我迄今为止必须完成的步骤:
rails new survey
<install the script stuff he includes>
rails g nifty:layout
rails g nifty:scaffold survey name:string
rake db:migrate
I updated routes.rb to point to home#index (rather then the welcome#index that it was) and deleted public/index.html
我更新了 routes.rb 以指向 home#index(而不是之前的welcome#index)并删除了 public/index.html
When I try to run rails server and go to my local host, I get the following error. uninitialized constant HomeController
当我尝试运行 rails 服务器并转到本地主机时,出现以下错误。未初始化的常量 HomeController
I'm lost and have no clue what this means.
我迷路了,不知道这意味着什么。
Can someone point me in the right direction?
有人可以指出我正确的方向吗?
EDIT:
编辑:
OK, So I fixed that problem, I guess where I'm confused is where should my routes point to to ge to see the survey that I just created using the above commands. right now I'm pointing to my home#index, where should that point to?
好的,所以我解决了这个问题,我想我感到困惑的地方是我的路线应该指向 ge 以查看我刚刚使用上述命令创建的调查。现在我指向我的家#index,它应该指向哪里?
Edit #2: Contents of Surveys_controller.rb
编辑 #2:Surveys_controller.rb 的内容
class SurveysController < ApplicationController
def index
@surveys = Survey.all
end
def show
@survey = Survey.find(params[:id])
end
def new
@survey = Survey.new
end
def create
@survey = Survey.new(params[:survey])
if @survey.save
flash[:notice] = "Successfully created survey."
redirect_to @survey
else
render :action => 'new'
end
end
def edit
@survey = Survey.find(params[:id])
end
def update
@survey = Survey.find(params[:id])
if @survey.update_attributes(params[:survey])
flash[:notice] = "Successfully updated survey."
redirect_to @survey
else
render :action => 'edit'
end
end
def destroy
@survey = Survey.find(params[:id])
@survey.destroy
flash[:notice] = "Successfully destroyed survey."
redirect_to surveys_url
end
end
回答by zsalzbank
With routes.rb pointing to home#index, it needs a HomeController in you app/controllers folder.
使用 routes.rb 指向home#index,它需要在您的 app/controllers 文件夹中有一个 HomeController。
If you follow the tutorial exactly, you can point to just survey#index. Take a look at surveys.rb in app/controllers to see what pages are available. They were generated with the niffty_scaffold script.
如果您完全按照教程进行操作,则可以指向 just survey#index。查看 app/controllers 中的surveys.rb 以查看哪些页面可用。它们是使用 niffty_scaffold 脚本生成的。
回答by onaclov2000
Turns out when you try to point to home#index, it needs to have something there, simply running
事实证明,当您尝试指向 home#index 时,它需要在那里有一些东西,只需运行
rails generate controller home index
fixes that problem.
解决了这个问题。
回答by stephenmurdoch
stick something like this in your application.html.erb file
在 application.html.erb 文件中粘贴类似的内容
<%= link_to "Home", root_path %>
<%= link_to "Surveys", surveys_path %>
The code will blow up if you don't have those routes working but otherwise you should be able to see your surveys by clicking on the button for them
如果您没有这些路线工作,代码将会爆炸,否则您应该能够通过单击它们的按钮来查看您的调查
Your routes.rb file should contain the following:
您的 routes.rb 文件应包含以下内容:
resources :surveys
root :to => "home#index"
You can view all surveys by going to localhost:3000/surveys
您可以通过访问 localhost:3000/surveys 查看所有调查

