【问题标题】:Rails routing error - uninitialized constant ArticlesControllerRails 路由错误 - 未初始化的常量 ArticlesController
【发布时间】:2016-11-07 10:35:53
【问题描述】:

我在以下帖子中阅读了问题的解决方案,但我无法将它们应用到我的案例中:

我正在开发一个测试博客,我想为新文章创建一个名为“new.html.erb”的页面。这是它的代码:

<h1 align="center">Create an article</h1>
<% end %>
<%= form_for @article do |f| %>
<p>
<%= f.label :title %><br/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br/>
<%= f.text_area :description %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>

我还创建了一个名为“articles.controller.rb”的控制器:

class ArticlesController < ApplicationController
  def new
    @article = Article.new
  end
end

我在 'routes.rb' 中添加了以下行

resources :articles

当我尝试在我的 rails 应用程序中访问 /articles/new 时,它显示:

未初始化的常量 ArticlesController

'$ rake routes' 给我以下输出:

      Prefix Verb   URI Pattern                  Controller#Action
        root GET    /                            pages#home
 pages_about GET    /pages/about(.:format)       pages#about
    articles GET    /articles(.:format)          articles#index
             POST   /articles(.:format)          articles#create
 new_article GET    /articles/new(.:format)      articles#new
edit_article GET    /articles/:id/edit(.:format) articles#edit
     article GET    /articles/:id(.:format)      articles#show
             PATCH  /articles/:id(.:format)      articles#update
             PUT    /articles/:id(.:format)      articles#update
             DELETE /articles/:id(.:format)      articles#destroy

这里是my app on Github

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    重命名你的控制器

    articles.controller.rbarticles_controller.rb

    您的控制器名称必须用下划线_ 分隔。

    【讨论】:

    • 工作。非常感谢。
    【解决方案2】:

    您的控制器名称错误。

    应该是articles_controller.rb 而不是articles.controller.rb

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      • 2012-05-04
      相关资源
      最近更新 更多