I need to display flash in the same view (:edit) after successfully updating an object. If I redirect to another action - everything works correctly. But when I need to stay in :edit - doesn't work. Can someone explain to me what is my mistake... Thanks!
I have the following snippet of code in my controller:
def edit
@setting = Setting.find_by(slug: current_user)
end
def update
@setting = Setting.find_by(slug: current_user)
if @setting.update(settings_params)
flash[:success] = I18n.t('admin.settings.edit.success')
else
flash[:danger] = I18n.t('admin.settings.edit.not_saved')
end
redirect_to edit_admin_setting_url(user: current_user)
end
routes.rb
:
scope ":user/" do
namespace :admin do
resource :setting, only: [:edit, :update]
end
end
And edit.html.erb
<% if flash[:success].present? %>
<div class="<%= classes + "alert-success" %>">
<%= icon("fas", "check") %>
<%= flash[:success] %>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<% end %>
I also tried:
flash.now[:success] = I18n.t('admin.settings.edit.success')
render :edit
Doesn't work either.