【问题标题】:Rails 5 - ActionController::RoutingError (uninitialized constant PeopleController)Rails 5 - ActionController::RoutingError(未初始化的常量 PeopleController)
【发布时间】:2017-08-05 14:01:55
【问题描述】:

我在 Heroku 日志中收到此路由错误,我不确定如何修复它。我按照seen here 的说明进行操作。

这是我的 routes.rb 文件:

Rails.application.routes.draw do
  resources :people, except: [:show]

  root to: "people#index"
end

控制器people_controller.rb:

class PeopleController < ApplicationController
  def index
    @people = Person.all
  end

  def new
    @person = Person.new
  end

  def create
    @person = Person.new(person_params)

    if @person.save
      redirect_to people_path, notice: "The person has been created!" and return
    end
    render 'new'
  end

  def edit
    @person = Person.find(params[:id])
  end

  def update
    @person = Person.find(params[:id])

    if @person.update_attributes(person_params)
      redirect_to people_path, notice: "#{@person.first_name} #{@person.last_name} has been updated!" and return
    end

    render 'edit'
  end

  def destroy
    @person = Person.find(params[:id])
    @person.destroy

    redirect_to people_path, notice: "#{@person.first_name} #{@person.last_name} has been deleted!" and return
  end
private
  def person_params
    params.require(:person).permit(:first_name, :last_name, :email, :notes)
  end
end

如果需要,这里是 person.rb 模型:

class Person
  include Mongoid::Document
  field :first_name, type: String
  field :last_name, type: String
  field :email, type: String
  field :notes, type: String
end

【问题讨论】:

  • 文件名是什么? Rails 要求文件名与类名匹配。
  • 文件名为people_controller.rb,模型为person.rb。
  • 请从您的日志文件中发布包含此路由错误的摘录。
  • 它在本地工作吗?

标签: ruby-on-rails ruby routing ruby-on-rails-5


【解决方案1】:

运行命令:

bundle exec rake routes

并检查路线。

【讨论】:

    【解决方案2】:

    我已经尝试过您在我的环境中所做的完全相同的事情,并且效果很好。我找不到代码有什么问题。

    除了查看服务器上的文件结构。 (可能是文件在错误的文件夹中)。

    我和@puneet18 在一起,它在本地对你有用吗?

    【讨论】:

    • 它在本地工作,但不在 Heroku 上。我意识到 git 没有添加一堆文件。我使用了 git commit -am 命令,但它似乎只在我分别执行 git add -A 然后 git push -m 时才起作用。回想起来似乎是一个愚蠢的错误。
    • 那么你现在整理好了吗?
    【解决方案3】:

    (代表 OP 发布).

    我没有在 Git 中正确地暂存我的文件,因此它没有将我的所有更改推送到 Heroku。哎呀! ¯\_(ツ)_/¯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 2013-08-25
      • 2016-01-16
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多