Home > Software design >  rails won't render error messages for failed validations
rails won't render error messages for failed validations

Time:09-23

I can't get rails to render any error messages. I have this code at the top of a form partial for a simple crud project.

 <% if @article.errors.any? %>
        
        <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>
        <ul>
            <% @article.errors.each do |error| %>
                <li><%= error.full_message %></li>
            <% end %>
        </ul>
        
    <% end %>

and these are the validations inside of the model

class Article < ApplicationRecord
    validates :title, presence: true, length: {minimum: 5, maximum:100}
    validates :body, presence: true, length: {minimum: 10, maximum: 300}
end

when I try to save a new article in the web browser with the incorrect title and body length nothing happens. when I try to save an article manually in the console it fails and the errors are displayed when I enter article.errors.full_messages.I don't know why the errors won't display in the browser. Any advice is appreciated

edited- added controller and routes code

articles_controller.rb

class ArticlesController < ApplicationController
    before_action :set_article, only: [:show,:edit,:update,:destroy]
    
    def show
        
    end

    def index
        @articles = Article.all 
    end

    def new 
        @article = Article.new
    end

    def edit
        
    end 

    def create 
       @article = Article.new(article_params)
       if @article.save
        flash[:notice] = "Article was created successfully!"
        redirect_to @article 

       else 
        render 'new'
       end
    end

    def update
        
        if @article.update(article_params)
            flash[:notice] = "Article was updated successfully!"
            redirect_to @article
        else
            render 'edit'
        end
    end

    def destroy 
        
        @article.destroy
        flash[:alert] = "Article was deleted successfully!"
        redirect_to articles_path
    end

    private

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

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

    def check_input

        
    end

    

        


end 

routes.rb

Rails.application.routes.draw do
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  
  root "pages#home"

  resources :articles
  
end

CodePudding user response:

Modify your articles controller create to:

  def create
    @article = Article.new(article_params)

    if @article.save
      redirect_to article_url(@article), notice: 'Article was successfully created.'

    else
      render :new, status: :unprocessable_entity

    end
  end 

Explanation: render :new will render the new template, but it will also set the status code to 200 (OK). This is not what you want. You want to set the status code to 422 (Unprocessable Entity). This will tell the browser that the request was valid, but the server was unable to process it. This is what you want to happen when the validations fail.

  • Related