javascript - load bootstrap modal on redirect -


i have bootstrap modal allows users sign in. want users have not authenticated have sign modal pop on whatever page at.

session controller

  def new     respond_to |format|       format.html       format.js     end   end 

session/new.js.erb

$("#modal-window").html("<%= escape_javascript(render 'sessions/new') %>"); 

if user hit controller want them see signin modal. figured try redirect them new_session_path. didn't work , got missing template error.

  def new     if current_user       @image = image.new     else       redirect_to new_session_path     end   end 

if send html request, should html back. rails looking html template , isn't finding one.

i recommend using before_action controllers or actions want restrict. require making html template sign in.

before_action :verify_user, only: [:new]  def verify_user   redirect_to new_session_path , return unless current_user end 

(or redirect_to root_path or somewhere , trigger modal on page load.)

as modal, listen clicks on links want restrict.

# users.js.coffee $ ->   $('body.not-signed-in a.require-sign-in').click, ->     $.get $('body').data('sign-in-path')     return false 

which means you'd have add classes in html.

<body class="<%= current_user ? 'signed-in' : 'not-signed-in' %>" data-sign-in-path="<%= new_session_path %>">   <%= link_to 'link', some_path, class: 'require-sign-in' %> 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -