Edw*_*ler 4 cookies login ruby-on-rails ruby-on-rails-3
我想将登录用户名存储在cookie中,该cookie将在2周左右到期.我还希望登录视图填充cookie中的用户名.没有"记住我"复选框或任何必要的身份验证代码.
基于下面的答案,这是我工作的伪代码:
# encoding: utf-8
class SessionsController < ApplicationController
skip_before_filter :authorize
def new
params[:username] = cookies[:username] unless cookies[:username].nil?
end
def create
if user
cookies[:username] = {:value => params[:username].downcase, :expires => 2.weeks.from_now }
end
end
end
Run Code Online (Sandbox Code Playgroud)
为清楚起见,省略了大部分代码.
只需使用cookie存储设置cookie,如下所示:
cookies.signed[:username] = { :value => "username", :expires => 2.weeks.from_now }
Run Code Online (Sandbox Code Playgroud)
.signed使cookie成为"防篡改".
在你看来,你可能想要这样的东西:
<%= f.text_field :username, :value => cookies.signed[:username] %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1263 次 |
| 最近记录: |