简体   繁体   English

Rails-如何要求用户登录以使用jquery供电的按钮

[英]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. 我对其进行了结构化,以便内容区域包含对象的多个属性的数据,并且jQuery选项卡仅隐藏/取消隐藏属性,以便它们与所选选项卡相对应。

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. 换句话说,所有数据都位于页面上,我正在使用jquery来更新div类以隐藏/取消隐藏数据的一部分。 There is no Ajax being used as a result. 结果没有使用Ajax。

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? 您如何检查用户是否仅使用类似jquery的按钮登录,以及如何执行类似调用弹出窗口以要求用户登录的操作?

Thanks! 谢谢!

Note---- 注意 - -

I am using Devise so I have access to those methods for checking if user is signed in 我正在使用Devise,因此可以访问那些方法来检查用户是否已登录

That's not jQuery deal! 那不是jQuery的交易! jQuery works with the data, that already passed from your server to client's browser. jQuery处理已经从服务器传递到客户端浏览器的数据。 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). 因此,jQuery中没有任何限制不会阻止邪恶用户查看您不希望他看到的页面(jQuery可以将其隐藏在我的祖母面前)。

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?" “ sign_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. 首先,我认为您做错了,因为您加载了所有内容,然后尝试使用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. 正如Parandroid所说的,您必须制作一些服务器端的东西。

Why don't you use the excellent Ruby on Rails' jQuery UI Helpers ? 您为什么不使用出色的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? %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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