简体   繁体   中英

redirect to a page after sign in , in ruby on rails

I am trying to automatically redirect my user to skip the login page if he or she has an active session my current code, which prompts the user with another page with a link in it. I tried to do redirect_to but ruby does not work with that method. Any suggestions?

   <% 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 %>

In your login page's controller (maybe session#new) you can check for the user's session like

if current_user or cookies[:session]

so within your new action

class SessionsController < ApplicationController
  def new
    redirect_to(root_url, alert: "already logged in") if current_user
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM