0

when I run my app I get this error :

(NoMethodError in Articles#new
Showing /home/ubuntu/workspace/yappy/qaucky/app/views/articles/new.html.erb where line #3 raised:

undefined method `model_name' for #<Article:0x007ff8e20e4b50>

Extracted source (around line #3):

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

this is the image (https://i.stack.imgur.com/kghdF.png)

Rails.root: /home/ubuntu/workspace/yappy/qaucky)

resources :articles

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

my new.html.erb file:

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

my articles controller file: class ArticlesController < ApplicationController

def new

@article = Article.new 

end





end 

my article.rb file :

class Article 


end

my routes.rb file :

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'

# Example of regular route:
#   get 'products/:id' => 'catalog#view'
kinny94
  • 376
  • 1
  • 5
  • 22
Shashi Yerra
  • 45
  • 11

1 Answers1

0

You cannot use form_for @article if @article doesn't respond to model_name and a couple of other methods that are used by Rails to determine and generate URLs.

Given that your question is tagged with ruby-on-rails I guess that you want to store articles in the database. ActiveRecord::Base provides this methods. Just inherit from it. Change

class Article 
end

to

class Article < ActiveRecord::Base
end

Note: This only works if there is a table named articles in your database.

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • i am getting an error that says : ActiveRecord::StatementInvalid in ArticlesController#new Could not find table 'articles' it is show me my articles controller file: class ArticlesController < ApplicationController def new @article = Article.new end end – Shashi Yerra Aug 15 '17 at 00:35
  • The error message `Could not find table 'articles'` is quite specific. There is no database table to store the articles you create with the form. Just create a database migration adding the table and the columns you need and run it. See the [Rails Guides about migrations](http://edgeguides.rubyonrails.org/active_record_migrations.html). – spickermann Aug 15 '17 at 05:15
  • i am trying to make articles from the UI will this still work – Shashi Yerra Aug 15 '17 at 20:47
  • i am a rails newbie so can you please explain that a bit clearer – Shashi Yerra Aug 15 '17 at 20:49