gpt4 book ai didi

ruby-on-rails - 如何在注册后立即登录用户?

转载 作者:数据小太阳 更新时间:2023-10-29 08:29:08 25 4
gpt4 key购买 nike

我知道我应该将代码放在用户 Controller 的创建操作中,但我不确定应该放什么代码。我还假设它应该在我的 session Controller 中调用创建操作,但我还是不确定如何...

顺便说一句,我在用户 Controller 的创建操作中尝试了 render :template => 'sessions/create',但是我在注册时遇到了这个错误:

Template is missing

Missing template sessions/create with {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:rjs, :rhtml, :erb, :rxml, :builder]} in view paths "/rubyprograms/dreamstill/app/views", "/rubyprograms/dreamstill/vendor/plugins/facebox_render/app/views"

这一切都在我的应用程序 Controller 中:

protected 
# Returns the currently logged in user or nil if there isn't one
def current_user
return unless session[:user_id]
@current_user ||= User.find_by_id(session[:user_id])
end


# Make current_user available in templates as a helper
helper_method :current_user

# Filter method to enforce a login requirement
# Apply as a before_filter on any controller you want to protect
def authenticate
logged_in? ? true : access_denied
end

# Predicate method to test for a logged in user
def logged_in?
current_user.is_a? User
end

# Make logged_in? available in templates as a helper
helper_method :logged_in?

def access_denied
respond_to do |format|
format.html do
flash[:alert] = "You must log in to peform this action."
redirect_to root_path
end

format.js do
render_to_facebox(:partial => 'sessions/login_box')
end
end
false
end

最佳答案

在你的 Controller 的某个地方,你有一些看起来像这样的东西:

user = User.new
# set attributes
user.save
render :template => 'sessions/create' # Probably based on your question

您需要做的就是将 session 更新为:

user = User.new
# set attributes
if(user.save)
session[:user_id] = user.id
# Send them somewhere useful
else
# Handle the error
end

一旦设置了 session[:user_id],他们就会登录。

关于ruby-on-rails - 如何在注册后立即登录用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5438606/

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