Ruby-on-rails 启动 Rails 服务器时出错:未定义的方法“配置”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22977051/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:32:10  来源:igfitidea点击:

Error launching Rails server: undefined method 'configure'

ruby-on-railsgemrailstutorial.org

提问by snorkelzebra

I'm new to rails and working through Hartl's tutorial. Everything was fine until I tried to do the tutorial a second time and created another project trying to use the latest version of rails. When I try to load the rails serverfrom the app folder I get the following error.

我是 Rails 新手,正在学习Hartl 的教程。一切都很好,直到我尝试第二次学习本教程并创建了另一个尝试使用最新版本 rails 的项目。当我尝试rails server从应用程序文件夹加载时,出现以下错误。

$ rails s
=> Booting WEBrick
=> Rails 4.0.4 application starting in development on
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/sierra/Desktop/sample_app_2/config/environments/development.rb:1:in
  `<top (required)>': undefined method `configure' for
  #<SampleApp2::Application:0x00000101a74610> (NoMethodError)

My Gemfile is directly from the Hartl tutorial:

我的 Gemfile 直接来自 Hartl 教程:

source 'https://rubygems.org'
ruby '2.1.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.4'

group :development, :test do
  gem 'sqlite3', '1.3.8'
  gem 'rspec-rails', '2.13.1'
end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
end

gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'rails_12factor', '0.0.2'
end

回答by Muhammad Ateq Ejaz

I resolved it by doing following step.

我通过执行以下步骤解决了它。

Step 1: go to Project_Root_Directory/config/environment/development.rb

第 1 步:转到 Project_Root_Directory/config/environment/development.rb

Change this line

改变这一行

Rails.application.configure do

To

Your_Rails_Application_Folder_name::Application.configure do

For example my rails project folder name is 'Spree_demo' so Your_Rails_Application_Folder_namein the following line:

例如,我的 rails 项目文件夹名称是“Spree_demo”,因此Your_Rails_Application_Folder_name在以下行中:

Your_Rails_Application_Folder_name::Application.configure do

will be replaced as

将被替换为

SpreeDemo::Application.configure do

Note: See underscore in your application folder name it gets removed.

注意:请参阅您的应用程序文件夹名称中的下划线,它将被删除。

Hope it works for you guys.

希望它对你们有用。

回答by Omar

First set Ruby version before Rails new

在 Rails new 之前先设置 Ruby 版本

I had the same problem and I tried the answer given and it had no impact. I even tried changing the name to get rid of the underscore, and it had no impact.

我遇到了同样的问题,我尝试了给出的答案,但没有任何影响。我什至尝试更改名称以去掉下划线,但没有任何影响。

The problem is that you did this:

问题是你这样做了:

$ rails new app_name

$ rails new app_name

But your ruby version was probably 2.1.1 or something else. You want to do:

但是您的 ruby​​ 版本可能是 2.1.1 或其他版本。你想做:

$ rvm 2.0.0

$ rvm 2.0.0

BEFORE you run the new app, and then when you set 2.0.0 in your Gemfile (as Hartl recommends) it falls into place.

在您运行新应用程序之前,然后当您在 Gemfile 中设置 2.0.0 时(如 Hartl 推荐的那样),它就位。

I don't know WHY this works, and I hope someone will shed light on it, but I can tell you that this worked better than the answer that is currently in the lead.

我不知道为什么会这样,我希望有人能解释一下,但我可以告诉你,这比目前领先的答案更有效。

回答by Debora Aline

That happened to me too. The problem was that I used one version of Rails to create the project. Then I changed the Gemfile to use another version of Rails and the system was using it to scaffold or run the server. Newbie problem! Using the same version consistently should solve the problem. :-)

这也发生在我身上。问题是我使用了一个版本的 Rails 来创建项目。然后我将 Gemfile 更改为使用另一个版本的 Rails,系统使用它来搭建或运行服务器。新手问题!始终使用相同的版本应该可以解决问题。:-)

回答by user3894023

I posted a (probably way too long) answer in a similar question: rails - NoMethodError: undefined method `configure' for FirstApp. This thread actually started me on the way to my eventual solution, so I thought I'd post here as well just in case it's helpful to anyone else.

我在一个类似的问题中发布了一个(可能太长了)答案:rails - NoMethodError: undefined method `configure' for FirstApp。这个线程实际上让我开始了我的最终解决方案,所以我想我也会在这里发帖,以防万一它对其他人有帮助。

From what I can tell, the problem occurs when the app/config/initializers/development.rb(and production.rb) files are generated for a new project using some newer versions of Rails (I'm not sure in which version it started, I only tested Rails 4.1.4). Mr. Hartl uses Rails 4.0.8 for his tutorial, and that's the highest version I tested in which the new syntax doesn't occur.

据我所知,当使用一些较新版本的 Rails 为新项目生成app/config/initializers/development.rb(和 production.rb)文件时会出现问题(我不确定它是从哪个版本开始的) ,我只测试了 Rails 4.1.4)。Hartl 先生在他的教程中使用 Rails 4.0.8,这是我测试过的最高版本,没有出现新语法。

In Rails 4.1.4, and maybe some other versions after 4.0.8, the first line in those files is generated as Rails.application.configure.dorather than, using a project called sample_appas an example, SampleApp::Application.configure.doas in 4.0.8.

在 Rails 4.1.4 以及 4.0.8 之后的其他一些版本中,这些文件中的第一行是生成的,Rails.application.configure.do而不是使用名为4.0.8的项目sample_app作为示例SampleApp::Application.configure.do

I'm new to Rails so I don't know why this syntax changed in newer versions. I'm assuming it's intentional and somehow better than the old way. Most likely, Mr. Hartl will take it in to account in future editions of his tutorial that are updated for versions of Rails which include this change.

我是 Rails 的新手,所以我不知道为什么这种语法在较新的版本中发生了变化。我假设它是故意的,并且比旧方式更好。Hartl 先生很可能会在他的教程的未来版本中考虑到这一点,这些版本针对包含此更改的 Rails 版本进行了更新。

Until then, see my other answer in the question I mentioned abovefor a more thorough explanation of how I got around it on Windows 7, but the tl;drof it is:

在那之前,请参阅我在上面提到问题中的其他答案,以更全面地解释我如何在 Windows 7 上解决它,但它的tl;dr是:

Make sure you're using the version of Rails specified for the tutorial (4.0.8) in your local repository/root development directory beforeyou create your new project.Updating your Gemfile after creating the project is still important, but it won't solve this problem if the files themselves were generated with a newer version of Rails. You'll have to go in and edit that line manually in that case, as other users have suggested.

创建新项目之前,请确保在本地存储库/根开发目录中使用为教程 (4.0.8) 指定的 Rails 版本。在创建项目后更新您的 Gemfile 仍然很重要,但如果文件本身是使用较新版本的 Rails 生成的,则它不会解决这个问题。在这种情况下,您必须进入并手动编辑该行,正如其他用户所建议的那样。

回答by pirate694

I had this issue when I messed around with my Gemfile. For example I have created the app using rails 4.0.2 or something like that then due to some errors I changed it to 4.1.1 that change cause this exact same problem in both development and production

当我弄乱我的 Gemfile 时,我遇到了这个问题。例如,我使用 rails 4.0.2 或类似的东西创建了应用程序,然后由于一些错误,我将其更改为 4.1.1,该更改导致开发和生产中出现完全相同的问题