Search code examples
ruby-on-railsrubynomethoderror

NoMethodError in Articles#new undefined method `model_name' for #<Article:0x000000035a3450>


I am having a problem while creating articles from the UI.

My task is to make a new page with "create an article" shown on the page and text fields with title and description and submit button when I press the submit button the article I typed in should be saved and when I go to the page where the articles are saved they should be there.

Error log:

this is my image of the error while I was running the app

This is my Cloud 9 code

routes.rb:

Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
# root 'welcome#index'
resources :articles

root 'pages#home'
get 'about', to: 'pages#about'

Articles controller (articles_controller.rb):

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

new.html.erb in articles folder in views:

<h1>Create an article</h1>
<%= form_for @article do |f| %>
<% end %>

Article model (article.rb) :

class Article 

end

Solution

  • Is your Article model a subclass of ActiveRecord::Base (ApplicationRecord from Rails 5.x)?

    class Article < ActiveRecord::Base
    
    end