作者:beijing750 | 来源:互联网 | 2023-02-10 15:58
Imwonderingwhatagoodwayistologauserinimmediatelyaftersigningup.Idontrequiree-ma
I'm wondering what a good way is to log a user in immediately after signing up. I don't require e-mail confirmation so there's no reason a user shouldn't expect this to automatically happen.
我想知道注册后立即登录用户有什么好办法。我不需要电子邮件确认,因此用户不应该期望这种情况自动发生。
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to welcome_url, notice: "Your user: #{@user.name} was successfully created! Please log in!" }
format.json { render json: @user, status: :created, location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
One problem I'm seeing is that I don't have a route to connect directly to the POST session#create. It looks like I should but there is no specific route
我看到的一个问题是我没有直接连接到POST会话#create的路由。它看起来像我应该但没有具体的路线
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
get 'logout' => :destroy
end
yields:
收益率:
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
GET /logout(.:format) sessions#destroy
For reference sake, here's my sessions#create method
为了参考,这是我的会话#create方法
def create
user = User.find_by_name(params[:name])
if user and user.authenticate(params[:password])
session[:user_id] = user.id
if session[:return_to]
redirect_to session[:return_to]
session[:return_to] = nil
return
end
redirect_to eval("#{user.role}_url")
else
redirect_to login_url, alert: "Invalid user/password combination"
end
end
Can I just hook directly into my session#create route from my user@create controller method as if the user is signing in except for the first time??
我可以直接挂钩到我的session #create route from my user @ create controller方法,就好像用户正在登录,除了第一次?
1 个解决方案