Search code examples
ruby-on-railsrubyruby-on-rails-3cloud9-idecloud9

I have a cloud 9 error while running the app in cloud 9


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.sstatic.net/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'

Solution

  • 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.