1
votes

I'm new to rails and looking for a bit of assistance, I've been going around in circles with for a while, I'm getting the following error when I run localHost:3000, Routing Error uninitialized constant ArticleController, I've been editing the routes.rb to be singular to plural but it seems to have caused more issues

routes.rb

  Rails.application.routes.draw do
   resources :articles, :comments
    root 'articles#index'

    resources :people
    get   'entries/sign_in' => 'entries#sign_in'
    post  'entries/sign_in' => 'entries#sign_in'

    get 'assignment_2/index' => 'assignment#index'

 end

and my article controller is as follows

 article_controller

class ArticleController < ApplicationController
  def index
    @article = Article.page(params[:page]).per(25)
  end
  def update
    @article = Article.find(params[:id])
    if @article.update(params.require(:article).permit(:title, :company, :url))
      redirect_to root_path
    else
      render 'edit'
    end
  end
  def create
    @article = Article.new(params.require(:article).permit(:title, :company, :url))
    if @article.save
      redirect_to root_path
    else
      render 'new'
    end
  end
  def show
    @article = Article.page(params[:page]).per(25)
  end
  def new
    @article = Article.new
  end

end

the actual error is showing as

 NameError in ArticlesController#index
 uninitialized constant ArticlesController::Articles
 Extracted source (around line #3):

 1 class ArticlesController < ApplicationController
 2   def index
 3     @article = Articles.page(params[:page]).per(25)
 4   end
 5   def update
 6     @article = Articles.find(params[:id])


   Rails.root: C:/Users/markf/Desktop/School/RAD/RAD_Assignment_2_s9971085/Assignment_2_app
   Application Trace | Framework Trace | Full Trace

  app/controllers/articles_controller.rb:3:in `index'
  actionpack (5.1.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
  actionpack (5.1.6) lib/abstract_controller/base.rb:186:in `process_action'
  actionpack (5.1.6) lib/action_controller/metal/rendering.rb:30:in `process_action'
  actionpack (5.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (5.1.6) lib/active_support/callbacks.rb:131:in `run_callbacks'
  actionpack (5.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (5.1.6) lib/action_controller/metal/rescue.rb:20:in `process_action'
  actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (5.1.6) lib/active_support/notifications.rb:166:in `block in instrument'
  activesupport (5.1.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  activesupport (5.1.6) lib/active_support/notifications.rb:166:in `instrument'
  actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (5.1.6) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
  activerecord (5.1.6) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
  actionpack (5.1.6) lib/abstract_controller/base.rb:124:in `process'
  actionview (5.1.6) lib/action_view/rendering.rb:30:in `process'
  actionpack (5.1.6) lib/action_controller/metal.rb:189:in `dispatch'
  actionpack (5.1.6) lib/action_controller/metal.rb:253:in `dispatch'
  actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
  actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:31:in `serve'
  actionpack (5.1.6) lib/action_dispatch/journey/router.rb:50:in `block in serve'
  actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `each'
  actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `serve'
  actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:844:in `call'
  rack (2.0.5) lib/rack/etag.rb:25:in `call'
  rack (2.0.5) lib/rack/conditional_get.rb:25:in `call'
  rack (2.0.5) lib/rack/head.rb:12:in `call'
  rack (2.0.5) lib/rack/session/abstract/id.rb:232:in `context'
  rack (2.0.5) lib/rack/session/abstract/id.rb:226:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/cookies.rb:613:in `call'
  activerecord (5.1.6) lib/active_record/migration.rb:556:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
  activesupport (5.1.6) lib/active_support/callbacks.rb:97:in `run_callbacks'
  actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
  web-console (3.6.2) lib/web_console/middleware.rb:135:in `call_app'
 web-console (3.6.2) lib/web_console/middleware.rb:30:in `block in call'
 web-console (3.6.2) lib/web_console/middleware.rb:20:in `catch'
 web-console (3.6.2) lib/web_console/middleware.rb:20:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
 railties (5.1.6) lib/rails/rack/logger.rb:36:in `call_app'
 railties (5.1.6) lib/rails/rack/logger.rb:24:in `block in call'
 activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `block in tagged'
 activesupport (5.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
 activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `tagged'
  railties (5.1.6) lib/rails/rack/logger.rb:24:in `call'
  sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
   actionpack (5.1.6) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
   actionpack (5.1.6) lib/action_dispatch/middleware/request_id.rb:25:in `call'
  rack (2.0.5) lib/rack/method_override.rb:22:in `call'
  rack (2.0.5) lib/rack/runtime.rb:22:in `call'
  activesupport (5.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
   actionpack (5.1.6) lib/action_dispatch/middleware/static.rb:125:in `call'
   rack (2.0.5) lib/rack/sendfile.rb:111:in `call'
  railties (5.1.6) lib/rails/engine.rb:522:in `call'
  puma (3.11.4) lib/puma/configuration.rb:225:in `call'
  puma (3.11.4) lib/puma/server.rb:632:in `handle_request'
 puma (3.11.4) lib/puma/server.rb:446:in `process_client'
  puma (3.11.4) lib/puma/server.rb:306:in `block in run'
 puma (3.11.4) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

 Request

  Parameters:

  None

  Toggle session dump
 Toggle env dump
  Response

  Headers:

  None

Above is before I renamed the articles_controller now the error message shows as follows

 ActionController::UnknownFormat in ArticlesController#index
 ArticlesController#index is missing a template for this request format        and variant. request.formats: ["text/html"] request.variant: [] NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.

Extracted source (around line #53):

 51
 52
 53
 54
 55
 56




           "That's what you'll get from an XHR or API request. Give it a shot."

         raise ActionController::UnknownFormat, message
        else
         logger.info "No template found for #{self.class.name}\##{action_name}, rendering head :no_content" if logger
         super

  Rails.root: C:/Users/markf/Desktop/School/RAD/RAD_Assignment_2_s9971085/Assignment_2_app

Any assistance would be greatly appreciated, like I said still learning.

2
Can you share the stacktrace of error?Jagdeep Singh
Did you tried plural name for controller? i.e. "class ArticlesController < ApplicationController"shlensky

2 Answers

0
votes

Controllers should always be in plural form. If you run bundle exec rake routes you will see that rails is looking for example method like articles#index:

    /config/routes.rb
    resources articles
    => rake routes
     Prefix   Verb       URI Pattern                    Controller#action
     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
              PUT        /articles/:id(.:format)        articles#update
              DELETE     /articles/:id(.:format)        articles#destroy

So these changes should fix the problem: - Rename your controller file article_controller.rb -> articles_controller.rb; - Rename your controller class name ArticleController -> ArticlesController.

0
votes

Your controller name is wrong.

Rename app/controllers/article_controller.rb to app/controller/articles_controller.rb

Changed the line class ArticleController < ApplicationController to

class ArticlesController < ApplicationController

Also, there is the difference between your log output and your articles_controller.rb file.

You should use Article and not Articles.

Model name is used in singular form and controller name in the plural form.

So, the controller will articles_controller but model will be Article.