I'm working on a Rails app that's using an external api for authentication. If the the same login credentials are used simultaneously the app has to alert the second user that there's already a session in progress. This needs to be a JavaScript alert with an 'ok' box, and the return has to be passed to the api as a flag to kill the first session.
I've got ideas for parts of it, but it's not coming together.
A hidden input in the login form that's triggered by the alert box?
I also tried this for the alert, but I'm unsure of how to pass 'msg' into the JavaScript, and this doesn't even address the params issue.
login_controller.rb
elsif body['ValidationMessage'] == 'OtherSessionActive'
flash[:alert] = auth_config['session_alert']
redirect_to login_path and return
else
login.html.erb
<div id='flash'>
<% flash.each do |name, msg| %>
<% if name == :alert %>
<script>
alert(#{msg})
</script>
<% else %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
<% end %>
</div>
Any ideas would be appreciated.
A way (but not good practice) is following:
<div id='flash'>
<% flash.each do |name, msg| %>
<% if name == :alert %>
<script type='text/javascript'>
$(function(){ // if you are using jquery
alert("<%= msg %>")
})
</script>
<% else %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
<% end %>
</div>
My preferred way is: use the following gem: https://github.com/gazay/gon
You can assign message to javascript object from controller. And can show alert directly from your javascript file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.