Having an issue in my Rails 7 app of alerts not showing up.
notices
, however, are.
I am displaying alerts in application.html.erb
using <%= alert %>
<%= render partial: "shared/navbar" %>
<div class="px-6">
<div class="mt-6"></div>
<div>
<% if notice %>
<div class="notification is-success is-light text-center">
<%= notice %>
</div>
<% end %>
<% if alert %>
<% debugger %>
<div class="notification is-danger is-light text-center">
<%= alert %>
</div>
<% end %>
<%= yield %>
</div>
</div>
I have added a "FAILED" logger to my controller action, which I am seeing in the Rails logs. This proves to me that we are indeed falling into the "else" condition, and the new
page is being rendered upon failure to create a new object. However, I do not see the alert message.
def create
@rescue = AnimalRescue.new(rescue_params)
respond_to do |format|
if @rescue.save
format.html { redirect_to animal_rescue_url(@rescue), notice: "Rescue was successfully created." }
format.json { render :show, status: :created, location: @rescue }
else
Rails.logger.info "FAILED"
format.html { render :new, status: :unprocessable_entity, alert: "Unable to create rescue." }
format.json { render json: @rescue.errors, status: :unprocessable_entity }
end
end
end
format.html { render :new, status: :unprocessable_entity, alert: "Unable to create rescue." }
# NOTE: not a render option ^^^^^^
:alert
and :notice
are options for redirect_to
method, which set the corresponding flash
message:
flash[:alert] = "Unable to create rescue."
Also, flash
will only display on the next request after the redirect. To display a flash message in the current request you have to use flash.now
:
format.html do
flash.now.alert = "Unable to create rescue."
render :new, status: :unprocessable_entity
end
In case your form is inside of the turbo frame, you'll need to display an alert from within the frame.