Home > Mobile >  Rails validation error message is not displaying
Rails validation error message is not displaying

Time:05-09

I have tried multiple methods to show error message from views, but it is not appearing.

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

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

  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>

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

<% end %>

_form.html.erb

<center>
<h1>New article</h1>

<%= render 'form' %>

<%= link_to 'Back', articles_path %>
</center>

new.html.erb

class Article < ActiveRecord::Base
    #attr_accessible :title, :text
    has_many :comments, dependent: :destroy

    validates :title, presence: true
end

article.rb

Rails.application.routes.draw do

root "articles#index"

  resources :articles do
    resources :comments
  end
end

routes.rb

class ArticlesController < ApplicationController
    http_basic_authenticate_with name: "deba", password: "12345", except: [:index, :show]

    def index
        @articles = Article.all
    end

    def new
      @article = Article.new
    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' #@article.errors, status: :unprocessable_entity
        end
    end

    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

    def destroy
        @article = Article.find(params[:id])
        @article.destroy
        redirect_to articles_path
    end

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

articles_controller.rb

Kindly help me. I tried it, validation is working as I can check that if there is no title it redirect me to new page without saving it but not appearing the error messages.

CodePudding user response:

The problem is with your redirecting - it causes the browser to initiate a new request and then you lose all the data (including the instance variables where the error is stored). Instead of redirecting you should simply render the "new" partial when article is invalid.

You haven't posted your controller code but this should give you a direction

if @article.save
    # whatever you wanna do if its valid
else
    render "new" # render the view without redirecting
end

CodePudding user response:

try to edit your controller:

class ArticlesController < ApplicationController
    before_action :initialize_article, only: [:new, :create]
        
        ...

    def new
    end

    def create
      if @article.update(article_params)
        redirect_to @article
      else
        render :new
      end
    end

    ....

    private

    def initialize_article
      @article ||= Article.new
    end
end
  • Related