Rails 5 - how to implement ajax flash message for devise -


i making rails5 blog app devise. in comments controller, check if user exists using "before_action :authenticate_user!".

so, if user logged in create comment method gets executed , server response gets send client via ajax. however, if user not logged in "before_action :authenticate_user!" check creates 401 authentication error in response ( can seen in browser log).

i want display flash message above comment box or somewhere on page user knows he/she can not enter comment without logged in.

there many similar questions have found seem confusing , none of solutions worked me. of them talk changing devise confg (e.g. config.http_authenticatable_on_xhr = false) or override authenticate_user method etc.

can explain possible show flash message warning user login add comment (ajax call). don't want automatic redirect ajax calls.

create flash method catch unauthorized error in jquery , show flash message :

@flash = (content, type) ->   content = "<div class='container'><div class=\"alert alert-#{type}\">#{content}</div></div>" if type?   $('#flash').clearqueue().hide().html(content).fadein('slow').delay(3000).fadeout('slow')  $(document).ajaxerror (e, xhr, settings) ->   flash("unauthorized", "danger") if xhr.status == 401 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -