0

I have a problem with my app, I will now give you the code to from the app and picture of the error, this is my task: I am supposed to create a web app from ruby on rails and the app should create articles and save them to the database.

This is the image of the error: https://i.stack.imgur.com/hYTkl.png

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'

article.rb:

class Article < ActiveRecord::Base

end

articles_controller.rb:

class ArticlesController < ApplicationController

   def new
     @article = Article.new 
   end
    def create
       #render plain: params[:article].inspect
    @article.save 
    redirect_to_articles_show(@article)
    end
    private 
    def article_params 
   params.require(:article).permit(:title, :description)


   end





end

new.html.erb:

Create an article

<%= form_for @article do |f| %>

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

    <%= f.text_field:title %>

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

</p>
<p>
    <%= f.submit %>

</p>
    <% end %>

my migration file:

class CreateArticles < ActiveRecord::Migration
  def change

      create_table :articles do |t|
        t.string :title
        t.text :description

    end
  end
end

my schema.rb:

ActiveRecord::Schema.define(version: 20170820190312) do

  create_table "articles", force: :cascade do |t|
    t.string "title"
    t.text   "description"
  end

end
Shashi Yerra
  • 45
  • 11

2 Answers2

0

Your @article in the create action is nil. Try this one

def create
  @article = Article.new(article_params)
  @article.save 
  redirect_to_articles_show(@article)
end
Ursus
  • 29,643
  • 3
  • 33
  • 50
0

You need to instantiate the object in the create method before saving it.

Try updating your create method like so:

def create
  @article = Article.new(article_params)

  @article.save
  redirect_to @article
end

I hope this helps!

Dan Kreiger
  • 5,358
  • 2
  • 23
  • 27