Search code examples
ruby-on-railsruby

Rails: param is missing or the value is empty: article


I am new with Rails and I began to make a web app following the rubyonrails.org tutorial.

My app is a blog with articles.. I implemented create and edit functions which worked pretty well but suddenly an error while trying to access http://localhost:3000/articles/2/edit in order to edit an article. The error is ActionController::ParameterMissing in ArticlesController#edit param is missing or the value is empty: articles

Here is my ruby code:

class ArticlesController < ApplicationController
  def index
    @articles = Article.all
  end

  def new
    @article = Article.new
  end

  def edit
    @article = Article.find(params[:id])
    if @article.update(article_params)
      redirect_to @article
    else
      render 'edit'
    end
  end

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

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

  private

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

The line targeted by the error alert is params.require(:articles).permit(:title, :text) I really don't know where the error can be because everything was ok 2 minutes ago...

Thank you for your help


Solution

  • You are trying to update the article in the edit method. So when you navigate to "articles/2/edit/" it tries to update the article 2. But you did not pass any params.

    I think what you probably want is:

    def edit
      @article = Article.find(params[:id])
    end
    
    def update
      @article = Article.find(params[:id])
      if @article.update(article_params)
        redirect_to @article
      else
        render 'edit'
      end
    end