简体   繁体   中英

Rails - how to require user to be logged in for a jquery powered button

In my application, I have a page with tabs that update the contents of a content area. I have it structured so that the content area holds data for multiple attributes of objects and the jquery tabs simply hide/unhide the attributes so that they correspond with the selected tab.

In other words, all the data is sitting on the page and I am using jquery to just update the div classes to hide/unhide parts of the data. There is no Ajax being used as a result.

My question is, how would I do something like requiring a user to be signed in to be able to click on the tabs besides the default tab? How do you check if the user is signed in with just a jquery button like this and how would you do something like call a popup to appear asking the user to sign in?

Thanks!

Note----

I am using Devise so I have access to those methods for checking if user is signed in

That's not jQuery deal! jQuery works with the data, that already passed from your server to client's browser. So no restriction in jQuery dont stop the evil user from viewing page that you dont want him to see (jQuery could just hide it from my grandma).

It's a server side task. In example:

<% if signed_in? %>
    <div id="secret_tab_for_signed_user">
        Secret for James Bond Here!
    </div>
<% else %>
    <div class="cool_popup">
        Nono, dr. Evil, you should register to view this little secret!
    </div>
<% end %>

"signed_in?" must be function in your app that checks if the user is signed up.

First, I think you are doing it wrong because you load everything and then you try to filter your data using jQuery. A simple "show source code" on your page and everything you hided is public.

As Parandroid said it, you have to make some server side stuff.

Why don't you use the excellent Ruby on Rails' jQuery UI Helpers ? You can then do it properly (example taken from the doc):

<% tab.create('tab_to_hide', 'Private Stuff') do %>
    # content
<% end unless @current_user.nil? %>

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