gpt4 book ai didi

ruby-on-rails - Ruby on Rails 记住我登录不工作

转载 作者:太空宇宙 更新时间:2023-11-03 16:47:48 24 4
gpt4 key购买 nike

我正在学习以下教程:

http://railscasts.com/episodes/274-remember-me-reset-password

并且刚刚尝试添加“记住我”功能,因此本教程最多只有 4 分钟左右。

一切似乎都很好,但是一旦我尝试登录,它就会返回主页,而且没有人登录。

用户.rb:

before_create { generate_token(:auth_token) 

def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end

session Controller :

def create
user = User.find_by email: params[:email]
if user and user.authenticate params[:password]
# 'logging in' is performed by saving a user's id in the session variable
#session[:user_id] = user.id
#cookies.permanent[:auth_token] = user.auth_token
if params[:remember_me]
cookies.permanent[:auth_token] = user.auth_token
else
cookies[:auth_token] = user.auth_token
end
# redirect to last page or root_path (products/index.html.erb')
redirect_to root_path
else
flash.now.notice = "Invalid user/password"
render :new
end
end


def destroy
# the act of logging out is performed by simply setting the key (:user_id)
# in the session hash to a value of nil (nothing)
#session[:user_id] = nil
cookies.delete(:auth_token)
# redirect to root_path (products/index.html.erb')
redirect_to root_path
end

应用程序 Controller :

def authenticate_user
# if session[:user_id]
# @current_user = User.find session[:user_id]
# else
# @current_user = nil
# end
if cookies[:auth_token]
@current_user ||= User.find_by_auth_token!(cookies[:auth_token])
else
@current_user = nil
end
end

def logged_in?
not session[:user_id].nil?
end

views/sessions/new.html.erb:

<%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
<!-- DRAW HTML CONTENT FOR PAGE -->
<%= form_tag sessions_create_path, :method => :get do %><div id='wrapper'>
<div id="contentWrapper">
<div id="contentWrapper">
<h1 class="page_header">THOR CINEMAS:<div class="file_selector"><span class="darker">LOG IN</span></div></h1>
<div id="content">
<!-- FULL PAGE CATEGORY CONTAINER -->
<div class='category'>
<div class='large_panel'>
<%= image_tag "thor_hammer.jpg",:size => "900x250" %>
<h1>Log IN:</h1><table>
<tr>
<td>
<table>
<tr>
<td width="300px">
<div class='info'>
<%= label_tag :email,'EMAIL:' %>
</div>
</td>
<td>
<div class='info'>
<%= label_tag :password, 'PASSWORD:' %>
</div>
</td>
<td></td>
<td></td>
</tr>

<tr>
<td style="padding-right:45px"><%= text_field_tag :email, params[:email] , :autofocus => true, :size => 42 %> </td>
<td style="padding-right:45px"><%= password_field_tag :password, params[:password], :size => 42 %></td>
<div class="field">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
<%= label_tag :remember_me %>
</div>
</tr>

</table><br><p style="color:red"><%= flash[:notice] %></p>
</td>
<td>
<table>
<tr>
<td>
<div class='info'>
OPTIONS:
</div>
</td>
<td>

</td>
<td></td>
<td></td>
</tr>

<tr>
<td><%= submit_tag "Login" %></br></td>
<td><%= link_to "Register", new_user_path %></td>
</tr>

</table><br>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
</div><% end %>
</body>
</html>

电影 Controller :

def index
@films = Film.all
if logged_in?
@colour = Perference.find_by(user_id: session[:user_id]).colour
end
end

谁能告诉我问题出在哪里。

最佳答案

您的问题是您仍在整个 Controller 中使用 session[:user_id],您需要将使用 session[:user_id] 更改为 cookie[:auth_token]。使用session[:user_id]作为当前用户id的地方,可以这样写:

user_id = User.find_by_auth_token!(cookies[:auth_token]).id

关于ruby-on-rails - Ruby on Rails 记住我登录不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29581220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com