DRing
DRing

Reputation: 7039

Getting flash[:notice] to show with ajax

Ive tried several different answers I got on here but cant get my flash notice to show on an ajax call. Here is my controller action:

def update_email_settings
    @user = User.find(params[:user_id])
    @user.update(user_params)
    @user
    flash[:notice] = "Changes Saved Successfully"
    respond_to do |format|
      format.js
    end
  end

The .js.erb file for it:

<% if flash[:notice].present? %>
    $("#email_setttings").html("<%= escape_javascript(render partial: 'email_settings', locals: { notice: flash[:notice] }) %>");
<% elsif flash[:alert].preset? %>
    $("#email_setttings").html("<%= escape_javascript(render partial: 'email_settings', locals: { notice: flash[:alert] }) %>");
<% end %>

And then here is the partial it renders

<div class="col-6">
    <% if notice %>
      <div class="alert alert-success alert-dismissible fade show" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <%= flash[:notice] %>
      </div>
    <% elsif alert %>
      <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <%= flash[:alert] %>
      </div>
    <% end %>
....

Upvotes: 1

Views: 1077

Answers (1)

Pardeep Saini
Pardeep Saini

Reputation: 2102

try this

def update_email_settings
@user = User.find(params[:user_id])
@user.update(user_params)
@user    
respond_to do |format|
  format.js { flash[:notice] = "Changes Saved Successfully"}
end

end

Upvotes: 1

Related Questions