Flash [: notice] does not work when using AJAX in Rails

I have tried different make flash [: notice] options that work without a reboot.

Stackoverflow gave me this - How do you handle a Rail flash drive with Ajax requests? but i cant find a solution that worked for me.

For example, added to my controller:

def create
  @entry = Entry.new(params[:entry])
  respond_to do |format|
  if @entry.save
    format.html { redirect_to @entry, notice: 'Entry was successfully created.' }
    format.js {
  flash.now[:notice] = 'Website was successfully created.'
 render action: 'create'
}
  else
    format.html { render action: "new" }
    format.js { render action: "new" }
  end
  end
end

create.js

   $('<%= j render @website %>').appendTo('#websites').hide().fadeIn();
  $(".alert").html("<%= escape_javascript(flash[:notice]) %>"); $(".alert").show(300);
  $("#new_website")[0].reset();

but it didn’t work.

Can someone tell me an understandable complete solution that worked for him?

+5
source share
2 answers

Are you making a typo on your js template file name? it should be create.js.erb but not create.js
and strictly follow fooobar.com/questions/75830 / ... and then make your changes to it.

+2

Ajax, - , . , JavaScript.

0

All Articles