使用 link_to 在 Ruby on Rails 中设置持久 cookie
我想使用 link_to 或按钮在访问者的浏览器中保存 cookie。它的唯一目的是显示一次性欢迎消息。
application_controller:
class ApplicationController < ActionController::Base
before_filter :first_time_visiting?
def first_time_visiting?
if session[:first_time].nil?
# session[:first_time] = 1
redirect_to "pages#welcome"
end
end
end
我不想像注释代码那样自动接受cookie,而是想将 session[:first_time] = 1
附加到pages#welcome 视图中的link_to 或button_to。我确信这是一项简单的任务,但我只是想知道我是否正确地处理了这个问题。我可以使用类似以下内容吗:
Pages#welcome:
<%= link_to("Continue", :controller => "home", :action => "index", :first_time => 1) %>
感谢您阅读我的帖子。
I would like to save a cookie with a visitor's browser using a link_to or a button. It's sole purpose is display a one time welcome message.
application_controller:
class ApplicationController < ActionController::Base
before_filter :first_time_visiting?
def first_time_visiting?
if session[:first_time].nil?
# session[:first_time] = 1
redirect_to "pages#welcome"
end
end
end
Instead of automatically accepting the cookie like the commented code, I'd like to attach session[:first_time] = 1
to a link_to or a button_to in the pages#welcome view. I'm sure this is a simple task but I'm just wondering if I'm going about this correctly. Can I just use something like:
Pages#welcome:
<%= link_to("Continue", :controller => "home", :action => "index", :first_time => 1) %>
Thank you for reading my post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://www.reddit.com/r/ruby/comments/fyprk/ rails_beginner_needing_help_with_setting_a_cookie/
一位好心的 Reddit 用户回答了我的问题。
http://www.reddit.com/r/ruby/comments/fyprk/rails_beginner_needing_help_with_setting_a_cookie/
A kind redditor answered my question.