简体   繁体   中英

Trigger modal instead redirect to /users/sign_in path, Rails 4 with devise

I searched for similar question but didn't find. All I could find was how to display login form when clicked on some specific link.

I want to use before_action :authenticate_user! At this point when I try to access advertisement show action that requires authentification it redirects to /users/sign_in path. Instead I want to show modal in the same window, where user can input his creditionals.

So far I have just prepared Bootstrap modal for login form.

<div class="modal fade" id="loginmodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">Login</h4>
      </div>
      <div class="modal-body">



      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Aizvērt</button>

      </div>
    </div>
  </div>
</div>

and Application_helper that allows to show login forms in any view.

module ApplicationHelper
     def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

Is this even possible ?

Thanks in advance!

You could just remove the authenticate_user before_action entirely and then simply do something like this in layouts/application.html.erb (with your modal code in that file too):

<% if !user_signed_in? %>
  $('#loginmodal').modal();
<% end %>

There are cleaner ways to do it on the view side but this should get you started.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM