-1

I am having a problem with my ruby on rails cloud 9 code while my task is to create an article from the UI and save it to the database when I hit submit.

This is my image of my problem:

enter image description here

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'
end

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| %>

    <p>
        <%= f.label :title %>
        <%= f.text_area :title %>
    </p>

<% end %>

Article model (article.rb) :

class Article < ActiveRecord::Base

end

I have done a migration and this is my migrate file :

class CreateArticles < ActiveRecord::Migration
  def change
    @article = Article.new

    create_table :articles do |t|
      t.string :title
    end
  end
end
engineersmnky
  • 25,495
  • 2
  • 36
  • 52
Shashi Yerra
  • 45
  • 11
  • The data in your migration file is wrong. I suggest you to read http://guides.rubyonrails.org/getting_started.html – Pavan Aug 21 '17 at 15:30
  • 2
    The indentation here is practically non-existent and that really impairs readability. Do make an effort to keep your code as clean and presentable as possible here when asking questions. It's not nice to make others struggle to understand what's going on. – tadman Aug 21 '17 at 15:31
  • I am so sorry I am new to stack overflow I do not intend for people to struggle, I have changed and tried my best to duplicate it from my code – Shashi Yerra Aug 21 '17 at 15:46

2 Answers2

3

Your migration seems to be missing the title column

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.string :title
    end
  end
end

Also your model should inherit from ApplicationRecord

class Article < ApplicationRecord
end

Or ActiveRecord::Base if your Rails version is less than 5

class Article < ActiveRecord::Base
end
Mihai Dinculescu
  • 19,743
  • 8
  • 55
  • 70
  • I have tried that but it is giving me this NoMethodError undefined method `title' for #
    – Shashi Yerra Aug 21 '17 at 15:33
  • now i am getting this : uninitialized constant ApplicationRecord – Shashi Yerra Aug 21 '17 at 15:40
  • 1
    You must be using an older Rails version. Inherit from `ActiveRecord::Base` instead. – Mihai Dinculescu Aug 21 '17 at 15:41
  • I am using rails version : Rails 4.2.5 (if this helps you) – Shashi Yerra Aug 21 '17 at 15:42
  • I have tried the ActiveRecord : : Base and now it is giving me the error : undefined method `title' for #
    – Shashi Yerra Aug 21 '17 at 15:45
  • did you rollback your previous migration and run the updated one? – Mihai Dinculescu Aug 21 '17 at 15:45
  • I tried that and I am getting this error with :rake db:migrate == 20170820190312 CreateArticles: migrating =================================== rake aborted! StandardError: An error has occurred, this and all later migrations canceled: Could not find table 'articles' – Shashi Yerra Aug 21 '17 at 15:56
  • @ShashiYerra do you still have this line `@article = Article.new` in the migration? If so remove it as it will try to ensure the table exists for reflection and type casting and it does not exist yet. Also it doesn't do anything and does not belong in a migration file. the migration posted in this answer is correctly what your migration file should look like – engineersmnky Aug 21 '17 at 16:22
0

You should remove the line @article = Article.new from migration file.

This would create a instance of Article using new keyword while you'll run the migration and it will be a nil instance, that's why you got the above error

Article id: nill

Because the last record was nil and you're looking for title of that record which primary key also nil.

Asmita
  • 183
  • 2
  • 14