Guilherme Luiz
Guilherme Luiz

Reputation: 127

flash[:notice] messages won't show

I'm making an app that creates and validates some inputs, but I'm having trouble to display error messages through flash[:notice] or success messages, it does validates though

subsidiaries controller

class  SubsidiariesController < ApplicationController
  def index
    @subsidiaries = Subsidiary.all
  end

  def show
    @subsidiary = Subsidiary.find(params[:id])
  end

  def new
    @subsidiary = Subsidiary.new
  end

  def create
    @subsidiary = Subsidiary.create(subsidiary_params)
    if @subsidiary.save
      redirect_to @subsidiary
    else
      flash[:alert] = 'Você deve informar todos os dados'
      render :new
    end
  end

  def edit
    @subsidiary = Subsidiary.find(params[:id])
  end

  def update
    @subsidiary = Subsidiary.find(params[:id])

    if @subsidiary.update(subsidiary_params)
      redirect_to @subsidiary
    else
      flash[:alert] = 'Você deve informar todos os dados'
      render :new
    end
  end

  private

  def subsidiary_params
    params.require(:subsidiary).permit(:name, :cnpj , :address)
  end
end

subsidiaries model

class Subsidiary < ApplicationRecord
  validates :name, :cnpj, :address, presence: true
  validates :cnpj, uniqueness: true, length: {is: 14}, numericality: { only_integer: true}
end

subsidiries/new.html

<h1><%= @subsidiary.name %></h1><br>
<h1><%= @subsidiary.cnpj %></h1><br>
<h1><%= @subsidiary.address %></h1><br>

<p><%= link_to "Editar", edit_subsidiary_path %></p>
<p><%= link_to "Voltar", subsidiaries_path %></p>

Upvotes: 0

Views: 125

Answers (1)

EdvardM
EdvardM

Reputation: 3102

You should explicitly assign :notice message somewhere in the controller, and also somewhere in the view show it. This latter could be in your default view, but it is nowhere present in the code shown.

Upvotes: 1

Related Questions