1
votes

I am new to Ruby on rails.I written the code in app/controllers/articles_controler.rb

def create
  @article = Article.new(article_params)

  @article.save
  redirect_to @article
end

private
  def article_params
    params.require(:article).permit(:title, :text)
  end

When i opened the rails server i got error as Routing Error uninitialized constant ArticleController.

In config/routes.rb I have the following code

Rails.application.routes.draw do

  get 'articles/new'

  resources :article 

  root 'welcome#index'
 end 
2
what do you have in your config/routes.rb? - Optimus Pette
do you a controller named class ArticlesController < ApplicationController ? - Wally Ali
Post your full controller code.And as @WaliAli said controller names should be plural. - Pavan
just like I thought, you have resources :article. and the name of your controller is class ArticlesController. right? - Wally Ali

2 Answers

5
votes

Controller names are plural

class ArticlesController < ApplicationController #notice, it is Articles

This means, in your config/routs.rb, you need to have a route that maps to articles (plural).

this surely means, in your config/routes.rb, you have a resources :article. and so the route is being mapped to a controller named Article, which you don't have it, and which is incorrect anyway. this is why you are getting Routing Error uninitialized constant ArticleController because it can't find a controller named Article (singular)

It should be resources :articles. that way, it is going to look for a controller name Articles

bottom line: controller names are pluralized. so check your namings.

1
votes

Although @Wali Ali is right, there is something else to contend

Controllers can be singular - the convention is for plural controller names, but we use singulars for the likes of exception and some others. If you wanted to keep the name singular, you could just do:

#config/routes.rb
resources :article # -> although not conventional, it will work

#app/controllers/article_controller.rb
class ArticleController < ApplicationController #-> name reflects route

Singular

Something else you may want to look at is singular routing resources. These work very simply:

#config/routes.rb
resource :article

#app/controllers/articles_controller.rb
class ArticlesController < ApplicationController

Although this is conventional, it means you'll have to keep the controller name as plural

This, coupled with @Wali Ali's explanation, should help you solve it