【问题标题】:Set email cookie in rails and find user according to the email cookie在rails中设置email cookie,根据email cookie查找用户
【发布时间】:2017-08-17 09:42:03
【问题描述】:

刚接触 Rails,我需要一些帮助!

我正在使用设计 gem,我正在尝试通过设置 cookies[:email] 来查找刚刚注册或尝试登录的当前用户。

我在 sessions_contoller.rb 和 registrations_contoller.rb 中有以下内容

def create
  cookies[:email] = params[:user][:email]
  #and some other code as well
end

def destroy
  super
  cookies.delete :email
end

下面的方法是根据邮箱找用户:

def current_client
  if cookies["email"].present?
    User.find_by_email(params[:email])
  end
end

如果我从浏览器检查,我可以看到电子邮件 cookie 在那里,但不确定如何从 rails c 检查。

但 current_client 方法似乎不起作用。

知道这里可能出了什么问题吗?

提前致谢!

【问题讨论】:

  • 找到解决方案?

标签: ruby-on-rails ruby ruby-on-rails-4 devise


【解决方案1】:

在设置cookie的值时,尝试如下设置:

cookies[:email] = {:value => params[:user][:email], :domain => :your_domain, :expires => 30.days.from_now}

您还可以通过在控制器中打印其值来检查 cookie,以验证它是否已设置。

【讨论】:

    猜你喜欢
    • 2012-11-28
    • 2011-01-14
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多