138weare
138weare

Reputation: 51

Error "param is missing or the value is empty: article"

I want to create an instance of "article" and the browser gives me an error

param is missing or the value is empty: article

Here my article controller

class ArticlesController < ApplicationController

    def index
        @article = Article.all
    end

    def new
          @article = Article.new
    end

    def create
        @article = Article.new(article_params)
     if @article.save
    redirect_to @article
     else
     render 'new'
  end
end

    def show
        @article = Article.find(params[:id])
    end

    private
  def article_params
    params.require(:article).permit(:title, :body)
  end
end

how fix?

Upvotes: 1

Views: 3260

Answers (3)

Elijah Logan
Elijah Logan

Reputation: 1

Try this code, it worked for me:

def article_params
  params.require(:articles).permit(:title, :body)
end

Upvotes: 0

dornseif
dornseif

Reputation: 11

This error indicates that the article parameter is missing or empty in the new form. Go into the project/views/new.html.erb and make sure that you are actually passing :article to the url: articles_path.

<%= form_for :*article*, url:  articles_path do |f| %>

I had a typo in article (accidentally had typed articles) which produced exactly the same error.

Upvotes: 1

Simone Carletti
Simone Carletti

Reputation: 176352

This is the method that raises the error.

def article_params
  params.require(:article).permit(:title, :body)
end

It means you are not passing such parameter to the controller. Make sure you have a form in the new/edit view and you are properly passing its values.

Upvotes: 0

Related Questions