1

Say I have a comment section and when a user enters a comment, if the user is not logged in, I want to show the sign in form in a modal box. Once the user is logged in, it should proceed to post the comment. I tried the following but it is redirecting the sign up page instead. Any suggestions? Thanks!

CommentsController

def create
   if !user_signed_in?          
      redirect_to(new_user_session_path, :remote=>true, :data => { :target => "#signin",  :toggle => "modal"})
    else
      ### continue with create
   end
end
Christian
  • 4,902
  • 4
  • 24
  • 42
user1256143
  • 11
  • 2
  • 3

1 Answers1

0

My suggestion is to have the modal inside a partial setted as hidden with display: none.

When users are not logged render it in your layout view (application.html.erb):

<% unless user_signed_in? %>
   <%= render :partial => "signin_modal" %>
<% end %>

When you want a link to be protected from unlogged user wrap it in a condition like this:

<% user_signed_in? ? url = your_create_comment_action_path : url = "#signin" %>
<%= link_to "Comment", url %>

Finally set an event to open signin modal when links with "#signin" href attribute are clicked. with jQuery:

$(document).on("click", "a[href='#signin']", function () {
    $("#SIGNIN_MODAL_ID").modal("show")
})

Of course you'll need to protect your controller too, adding a before_filter rule for your create comment action.

before_filter :check_unsigned_user, :only => [:create]

def check_unsigned_user
   unless user_signed_in?
      # make what you want
   end
end
marquez
  • 737
  • 4
  • 10
  • but this way every link to a private area I need to check if ther user is logged or not. Is there a way to do it automatically? maybe just inside the before_filter method. @marquez – Osny Netto Oct 03 '13 at 14:35