简体   繁体   English

Ruby on Rails 4中的“ NameError:未初始化的常量新闻通讯”,但未在开发中

[英]“NameError: uninitialized constant Newsletter” in Heroku, but not in development, Ruby on Rails 4

I have an app on Heroku. 我在Heroku上有一个应用程序。 I added a new model and a controller for "newsletter". 我添加了一个新模型和一个用于“新闻通讯”的控制器。 In development, everything works fine. 在开发中,一切正常。 Then I ran these lines of code: 然后,我运行了以下代码行:

git commit -a -m "added newsletter"
git push origin master
git push heroku master
heroku run rake db:migrate

But after trying to get the index page, I got this message: 但是在尝试获取索引页之后,我得到了以下消息:

The page you were looking for doesn't exist.

You may have mistyped the address or the page may have moved.

I ran heroku run rails console and typed in: Newsletter.connection 我运行了heroku run rails console并输入: Newsletter.connection

irb(main):001:0> Newsletter.connection
NameError: uninitialized constant Newsletter
        from (irb):1
        from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
        from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
        from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
        from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
        from /app/bin/rails:9:in `require'
        from /app/bin/rails:9:in `<main>'
irb(main):002:0> 

Then I ran Heroku logs : 然后我运行了Heroku logs

2016-02-14T20:24:36.918613+00:00 heroku[router]: at=info method=GET path="/newsletters" host=evening-basin-18662.herokuapp.com request_id=a7acad04-63e4-468f-9eac-bfec23c72773 fwd="87.245.109.31" dyno=web.1 connect=0ms service=13ms status=404 bytes=1829
2016-02-14T20:24:36.919403+00:00 app[web.1]: Started GET "/newsletters" for 87.245.109.31 at 2016-02-14 20:24:36 +0000
2016-02-14T20:24:36.924135+00:00 app[web.1]: 
2016-02-14T20:24:36.924138+00:00 app[web.1]: ActionController::RoutingError (uninitialized constant NewslettersController):

Where did I go wrong? 我哪里做错了? Thanks in advance! 提前致谢!

Edit: models/newsletter.rb 编辑:models / newsletter.rb

class Newsletter < ActiveRecord::Base
end

controllers/newsletters_controller.rb 控制器/ newsletters_controller.rb

class NewslettersController < ApplicationController
  def index
    @newsletters = Newsletter.all
  end

  def new
    @newsletter = Newsletter.new
  end

  def create
    @newsletter = Newsletter.new(newsletter_params)
    if @newsletter.save
      # @user = User.find(current_user.id)
      #ExampleMailer.sample_email(@user).deliver
      redirect_to newsletters_path
    else
      render 'new'
    end
  end

  private

  def newsletter_params
    params.require(:newsletter).permit(:name)
  end


end

config/routes.rb 配置/ routes.rb中

resources :newsletters

Edit 2: 编辑2:

Solved by Frederich Cheung. 由张国荣解决。

Frederick Cheung asked: "Did you definitely commit newsletter.rb?" 张学友问:“您确实提交了newsletter.rb吗?”

Then, I did this: 然后,我这样做:

git add .
git commit -m "add newsletter"
git push origin master
git push heroku master

And after that, it worked. 在那之后,它起作用了。

Because it is due to an error in git handling, here a more verbose explanation to what Michael Cruz already pointed out: 因为这是由于git处理中的错误所致,所以这里对迈克尔·克鲁兹已经指出的内容进行了更为详细的解释:

For more info have a look at the git commit documentation 有关更多信息,请参阅git commit文档。

git commit -a same as git commit-

git commit --all git commit --all

Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected . 告诉命令自动暂存已修改和删除的文件 ,但是您未告知Git的新文件不受影响

For more info have a look at the git add documentation 有关更多信息,请查看git add文档

git add git添加

This command [git add] updates the index using the current content found in the working tree, to prepare the content staged for the next commit. 此命令[git add]使用在工作树中找到的当前内容更新索引,以准备下一次提交的内容。 It typically adds the current content of existing paths as a whole , but with some options it can also be used to add content with only part of the changes made to the working tree files applied, or remove paths that do not exist in the working tree anymore. 它通常会整体添加现有路径的当前内容 ,但是通过某些选项,它也可以用于添加仅对工作树文件进行部分更改的内容,或者删除工作树中不存在的路径了。

Is my file tracked? 是否跟踪我的文件?

To find out if your file is tracked by git you can run git status myFileName.rb . 要了解git是否跟踪您的文件,可以运行git status myFileName.rb If it returns an error it is not tracked yet. 如果返回错误,则尚未跟踪。

Frederick Cheung asked: "Did you definitely commit newsletter.rb?" 张学友问:“您确实提交了newsletter.rb吗?”

Then, I did this: 然后,我这样做:

git add .
git commit -m "add newsletter"
git push origin master
git push heroku master

And after that, it worked. 在那之后,它起作用了。 (Weird, because I'm pretty sure, I ran git commit -a -m "add newsletter" ) (很奇怪,因为我很确定,我运行了git commit -a -m "add newsletter"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM