Rails 3:如何判断用户之前是否访问了某个页面?(Rails 3: How to tell if a user has visited a page before?)
我有一个Rails应用程序(使用Authlogic进行身份验证)和一个简单的jquery动画,我只想在第一页加载时运行一次。 我知道关键是检查cookie或其他东西,以查看用户之前是否访问过该页面。 请原谅我的n00bness,我对HTTP cookie或会话知之甚少。
那么,查看访问用户(即使他们还没有登录)是否第一次查看页面的最佳方法是什么?
编辑:好的,所以我意识到我并不完全清楚。
我花了好几个小时查看类似的问题,阅读Rails API文档中的cookie和会话,我仍然无法想象如何实现访问? 我的网站中每个页面的功能,只有在用户第一次访问该页面后才会设置为“true”。 我查看了所谓的“重复”问题Rails检测用户的第一次访问和相应的答案,仍然无法弄明白。
这是我的“页面”控制器:
def home @title = "Home" end def contact @title = "Contact Us" end
DD
我的jquery javascript做了一个简单的动画:
$(document).ready(function() { if (!$.cookie('visited')) { $('.title .flying-text').css({opacity:0}); $('.title .active-text').animate({ opacity:1, marginTop: "-150px", }, 5000); } });
我只希望它显示用户之前是否没有访问过该页面。 我不知道如何在Rails中正确设置cookie,也不知道如何设置它,也不知道如何确保动画脚本可以访问完全相同的cookie值。 有人可以帮我一把吗?
〜丹
I have a Rails app (using Authlogic for authentication) with a simple jquery animation that I only want to run once upon first page load. I know that the key is to check cookies or something to see if the user has visited the page before. Please forgive my n00bness, I know little to nothing about HTTP cookies or sessions.
So, what's the best way to see if a visiting user (even if they haven't logged in) is viewing a page for the first time?
EDIT: Ok, so I realize I wasn't being entirely clear.
I've spent hours looking at similar questions and reading the Rails API Docs for cookies and sessions and I still can't visualize how to implement a visited? function for each page in my site that will only be set to "true" after the user has visited the page the first time. I looked at the supposed "duplicate" question Rails Detect If User's Very First Visit and the respective answers and still can't figure it out.
Here's my "Pages" controller:
def home @title = "Home" end def contact @title = "Contact Us" end
dd
And my jquery javascript that does a simple animation:
$(document).ready(function() { if (!$.cookie('visited')) { $('.title .flying-text').css({opacity:0}); $('.title .active-text').animate({ opacity:1, marginTop: "-150px", }, 5000); } });
I only want it to show if the user HAS NOT visited the page before. I have no idea how to properly set a cookie in Rails, nor where to put it, nor how to make sure that the animation script can access that exact same cookie value. Can someone give me a hand?
~Dan
原文:https://stackoverflow.com/questions/4994673
最满意答案
适配器类的例子:
public class AdapterClass extends BaseAdapter { private List _list; public AdapterDirectories(List list){ // Constructor with good parameters _list = list; } @Override public int getCount() { return _list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; CellHolder cellHolder = null; // get if (convertView == null) { view = _inflater.inflate(R.layout.list_item, null); cellHolder = new CellHolder(); cellHolder.textTitle = (TextView) view.findViewById(R.id.text_view_title); view.setTag(cellHolder); } else { cellHolder = (CellHolder) convertView.getTag(); } // set cellHolder.textTitle.setText(""); return view; } public class CellHolder { public TextView textTitle; } }
将适配器设置为列表视图:
_listView.setAdapter(_adapter);
Exemple of adapter class:
public class AdapterClass extends BaseAdapter { private List _list; public AdapterDirectories(List list){ // Constructor with good parameters _list = list; } @Override public int getCount() { return _list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; CellHolder cellHolder = null; // get if (convertView == null) { view = _inflater.inflate(R.layout.list_item, null); cellHolder = new CellHolder(); cellHolder.textTitle = (TextView) view.findViewById(R.id.text_view_title); view.setTag(cellHolder); } else { cellHolder = (CellHolder) convertView.getTag(); } // set cellHolder.textTitle.setText(""); return view; } public class CellHolder { public TextView textTitle; } }
setting the adapter to the list view :
_listView.setAdapter(_adapter);
相关问答
更多-
新的AuthorizeActionLink扩展方法。 根据需要超载。 public static MvcHtmlString AuthorizeActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes) { if (HasActionPermission(helper, actionNam ...
-
适配器类的例子: public class AdapterClass extends BaseAdapter { private List _list; public AdapterDirectories(List list){ // Constructor with good parameters _list = list; } @Override public int getCount() { ...
-
只是返回虚假,停止“吃”事件。 public boolean onKey(View view, int keyCode, KeyEvent event) { handleListViewBehavior(); return false; } 有关说明,请阅读文档 Returns True if the listener has consumed the event, false otherwise. Just return false, to stop "e ...
-
覆盖__getattr__应该很好 - __getattr__只被称为最后的手段,即如果实例中没有匹配名称的属性。 例如,如果您访问foo.bar ,则只有在foo没有属性称为bar情况下,才会调用__getattr__ 。 如果属性是您不想处理的AttributeError ,请引发AttributeError : class Foo(object): def __getattr__(self, name): if some_predicate(name): ...
-
您可能会在父管理器中混乱使用nextFocus()来跳过中间的VFM。 也许还会覆盖onFocus() ,中间就可以转到下一个。 我知道应该通过将其设置为不可聚焦来执行此操作,但可能会覆盖isFocusable()以返回false。 I figured out a solution by overriding the navigationMovement function in the parent Manager, something like this: protected boolean naviga ...
-
ToolStripSplitButton有两个点击处理程序。 一个叫做“Click”,另一个叫做“ButtonClick”。 构造函数中的一个是“Click”处理程序,并且无论您点击哪个控件都会触发。 “ButtonClick”处理程序仅在单击按钮时触发,而不是箭头。 尝试这个: var button = new ToolStripSplitButton("text","path to image"); button.ButtonClick += clickEventHandler; The ToolSt ...
-
如何覆盖?(How to override?)[2023-08-31]
从其他类继承方法(例如来自Object public String toString() )时,要覆盖它所需要做的就是在类中定义自己的public String toString() 。 您已经在代码中实际执行了此操作,并且已成功覆盖了Person所有实例的toString() 。 您没有看到预期结果的原因是您调用的toString()和equals()方法来自String类,而不是来自Person类。 这是因为您在String对象上调用它们,而不是Person对象 - 并且这两个类不是分层相关的(除了它们 ... -
是否可以擦除或覆盖zsh的prexec()错误行为?(Is it possible to erase or override zsh's prexec() error behavior?)[2023-06-15]
正确答案不是使用pre *函数而是使用command_not_found_handler函数。 这也适用于较新版本的bash。 感谢@chepner和@“JanKrüger”为解决这个问题所做的努力。 function command_not_found_handle { if [[ "$1" =~ "^(https|http|git|(git\\+)?ssh)://.*\$" ]]; then echo "Do something: $1" ... -
如果要覆盖某些方法,则必须创建一个extends WebView的自定义WebView类。 它看起来像这样: public class CustomWebView extends WebView { public CustomWebView(Context context) { this(context, null); } public CustomWebView(Context context, AttributeSet attrs) { thi ...
-
在我在我的问题中提到的应用程序中,没有覆盖默认行为。 这是我的错误,我采取了错误的方式。 使用Handler可以实现相同的功能。 在我的应用程序中,我使用了一个单击按钮启动的服务 。 在服务内部,我曾经使用过处理程序。 下面,我发布了代码片段。 public class DialerService extends Service { ActivityManager am; List
mAppProcessInfosList; private Runnable m ...