CreativeMinds
CreativeMinds

Reputation: 343

How to solve "undefined local variable or method `article' for #<ArticlesController:0x9d00548>" in ruby on rails

I'm new in ruby on rails web development. Now I try to create simple form using ruby on rails.

But after submitting the form it shows following error,

NameError in ArticlesController#create

undefined local variable or method `article` for #<ArticlesController:0x9d00548> Did you mean? article_url

Request

Parameters:

{"utf8"=>"✓",
"authenticity_token"=>"vUOVlqSQTJMHGT11RhZKv4jfcKgEyxnHXCMiHSeaUI7ghsnoZ4vsjZ/QmM2d/MvLLV0YFJ1UIn61RTgJEcgVvA==",
"article"=>{"itle"=>"Text heading", "text"=>"Text subject"},
"commit"=>"Save Article"}

Response
Headers:

None

My code part is,

new.html.erb

<h1>New Article</h1>

<%= form_with scope: :article, url: articles_path, local: true do |form| %>
    <p>
        <%= form.label :itle %><br>
        <%= form.text_field :itle %>
    </p>
    <p>
        <%= form.label :text %><br>
        <%= form.text_field :text %>
    </p>

    <p>
        <%= form.submit %>
    </p>
<% end %>

Article Controller

class ArticlesController < ApplicationController
    def new

    end

    def create
        @article = article.new(params[:article].inspect)

        @article.save
        redirect_to @article
    end
end

route

Rails.application.routes.draw do
  get 'welcome/index'

  resources :articles
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'welcome#index'
end

If I print the input value using this below line,

render plain: params[:article].inspect

It shows following message,

<ActionController::Parameters {"title"=>"First Article!", "text"=>"This is my first article."} permitted: false>

But I use article.new(params[:article].inspect) this line is does not work.

I check my code but I have no idea how to fix this issue.

If anyone help me to find what mistake I done my coding

I implement my code by using this page https://guides.rubyonrails.org/getting_started.html

Upvotes: 0

Views: 1244

Answers (1)

Ursus
Ursus

Reputation: 30071

Move from

@article = article.new(params[:article])

to

@article = Article.new(params[:article])

You need to use the class to create objects. Also, yes, article does not exist

Upvotes: 1

Related Questions