jeudi 9 avril 2015

Create member login using Rails 3

I have a doubt regarding member login using Rails 3.Let me to explain below my plan about creating member by Admin .


I have already done the admin login(views/admins/index.html.erb).Only Admin user can create member after login inside home page.As my login page and login method will remain same. Member also can login from that same page.But i want to create new model for the member(lets say 'User') so how can i differentiate both model in the same method(i.e-login).I am explaining my code below.


views/admins/index.html.erb



<div id="login">
<div id="triangle"></div>
<h1>Log in</h1>
<%= form_for :admin,:url => {:action => "login" ,:controller => "sessions" } do |f| %>
<%= f.email_field :email,placeholder:"Enter your email" %>
<%= f.password_field :password,placeholder:"Enter your password" %>
<%= f.submit "Log in" %>
<% end %>
<% if @admin.try(:errors).try(:any?) %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this post from being saved:</h2>

<ul>
<% @admin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</div>


sessions/sessions_controller.rb



class SessionsController < ApplicationController
def login
@admin=Admin.authenticate(params[:admin][:email], params[:admin][:password])
if @admin
session[:user_id]=@admin.id
cookies.signed[:user_id]=@admin.id
flash[:notice]="Login Successfull"
flash[:color]="valid"
redirect_to :action => 'index', :controller => "homes"
else
flash[:notice]="Login failed"
flash[:color]="invalid"
render 'admins/index'
end
end
def removeadmin
session[:user_id] = nil
cookies.delete :user_id
flash[:notice]="user logged out successfully"
flash[:color]="valid"
redirect_to :action => 'index', :controller => 'admins'
end
end


model/admin.rb



class Admin < ActiveRecord::Base
attr_accessible :email, :name, :password, :phoneno
EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates_length_of :password, :in => 6..20, :on => :create
def self.authenticate(email, password)
admin = find_by_email(email)
if admin && admin.password==password
admin
else
nil
end
end
end


homes/index.html.erb



<% if current_admin %>
<div class="header">
<div class="navbar-header">Swargadwar, Puri Municipality,govt of odisha</div>
<div class="nav navbar-top-links navbar-right">
<div class="image"></div>
</div>
<div class="name-div">
Logged in as:<%= current_admin.email %>
</div>
</div>
<div class="menu-div">
<div id="leftsidebtn">
<ul>
<li><a href="#">Create User</a></li>
<li><a href="#">Scan Report</a></li>
<li><a href="#">View and Payment Report</a></li>
<li><a href="#">Payment Validate</a></li>
<li><a href="/sessions/removeadmin">Log Out</a></li>
</ul>
</div>
</div>
<div class="content-div">
</div>
<% end %>


I want to create another model for member(i.e-User) and do the authentication inside user.rb for user/member loin but my login method is same.Please help me how can I differentiate Admin and User model after putting email and password on same login page from login method. Please help me.


Aucun commentaire:

Enregistrer un commentaire