dimanche 28 mai 2017

how to Redirect a page on successful sign in rails devise and after verify user== "teachers"

redirect to a specific page on the successful sign in. anyone can advise me how to redirect an after verify (userid == teacher ).if userid is a teacher then redirect teacher otherwise student page pbl01/config/routes.rb

Rails.application.routes.draw do
devise_for :users, :controllers => {
:registrations => 'users/registrations'
}
resources :import,only: [:index,:create]
root 'root#index'

# For details on the DSL available within this file, see 
http://ift.tt/GVpneB
end

pbl01/app/controllers/users/sessions_controller.rb

class Users::SessionsController < Devise::SessionsController

def new

end

def create
@user = User.new(user_params)
if @user.save
@userid = User.find_by(userid:params[:userid])
if @userid.userid == 'teacher'
  redirect_to root_path
else
 redirect_to timesset_path

end
else
  flash[:notice] = "Invalid Username or Password"
  flash[:color]= "invalid"
  render "new"  
   end

end

pbl01/app/models/user.rb

require 'csv'
class User < ApplicationRecord
validates :userid, :presence => true,
validates :password, :presence => true,


devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

def email_required?
false
end
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|

  users = find_by(userid: row["userid"]) || new

  users.attributes = row.to_hash.slice(*updatable_attributes)
  users.password= row["password"]

  users.save!
end
end


def self.updatable_attributes
["userid", "name", "email"]
 end
end

pbl01/app/views/devise/sessions/new.html.erb

<h2>Log in</h2>

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
 <% if @user.errors.any? %>
<div class="field">
<%= f.label :userid %><br />
<%= f.text_field :userid, autofocus: true %>
</div>

<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
<% for message_error in @user.errors.full_messages %>
  <li>* <%= message_error %></li>
<% end %>
</div>

<% if devise_mapping.rememberable? -%>
<div class="field">
  <%= f.check_box :remember_me %>
  <%= f.label :remember_me %>
</div>
<% end -%>

<div class="actions">
 <%= f.submit "Log in" %>
 </div>
 <% end %>

 <%= render "devise/shared/links" %>

Aucun commentaire:

Enregistrer un commentaire