user705828
user705828

Reputation: 31

How show flash error messages in Rails 3 using Ajax and jQuery?

My controller is:

def create
  @group = Group.new(params[:group])

  @group.company=current_user.company
  respond_to do |format|
    if @group.save
      format.html { redirect_to(@group, :notice => 'Group was successfully created.') }
      format.js
    else
      flash.now[:error][email protected]_messages
      format.html { render :nothing => true }
      format.js
    end
  end
end

create.js.erb is:

$('<%= escape_javascript(render(:partial => @group))%>').appendTo('#groups');
$("#new_group")[0].reset();

Upvotes: 1

Views: 4652

Answers (2)

shajin
shajin

Reputation: 3264

$(".flashnotice").html("<%= escape_javascript(flash[:notice]) %>");  
$(".flashnotice").show(300);

You have to add these two lines in your create.js.erb file. Replace .flashnotice with the selector for your flash HTML element; e.g., the class name of your flash <div>.

Upvotes: 7

chrismealy
chrismealy

Reputation: 4950

I think you just need to say <%= flash[:notice] %> somewhere in your erb file.

Upvotes: -4

Related Questions