[英]redirect to a page after sign in , in ruby on rails
我正在嘗試自動重定向我的用戶,如果他或她當前會話處於活動狀態,則跳過登錄頁面,這會提示用戶另一個帶有鏈接的頁面。 我嘗試做redirect_to,但是ruby不適用於該方法。 有什么建議么?
<% if signed_in? %>
<%= link_to "Users", 'users/show' %>
<% else %>
<%= form_for(:session, url: sessions_path) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<br>
<%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
<% end %>
在登錄頁面的控制器中(可能是session#new),您可以檢查用戶的會話,例如
if current_user
或cookies[:session]
所以在你的new
動作中
class SessionsController < ApplicationController
def new
redirect_to(root_url, alert: "already logged in") if current_user
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.