I want to add remember me feature in my login form using Rails 3.I can store session id and cookie.Please check some of my below code and please help me to add some stuff inside view as well as controller page.
views/homes/member.html.erb
<% if current_user %>
<div class="totaldiv">
<div class="navdiv"><span>STUDENT INFORMATION</span><span>Logged in as <%= current_user.email %></span></div>
<div class="wrapper">
<div id="leftsidebtn">
<ul>
<li><a href="/homes/issuebooks">Book issue</a></li>
<li><a href="/homes/availablebooks">Books Available</a></li>
<li><a href="/homes/magazines?user_id=<%= current_user.id %>">Magazines Purchase</a></li>
<li><a href="/homes/newspaper">Newspaper Purchase</a></li>
<li><a href="/homes/userprofile?id=<%= current_user.id %>">Profile settings</a></li>
<li><a href="/homes/blog">My Blog</a></li>
<li><a href="/sessions/removeuser">Log Out</a></li>
</ul>
</div>
</div>
<div class="restdiv" id="ex3" >
<center>
</center>
</div>
</div>
<% else %>
<div class="totaldiv">
<div class="navdiv"><span>STUDENT INFORMATION</span></div>
<div class="wrapper">
<div id="leftsidebtn">
<ul>
<li><a href="/homes/registration">Registration</a></li>
<li><a href="/homes/index">Back</a></li>
</ul>
</div>
</div>
<div class="restdiv" id="ex3" >
<center>
<div class="studentlogin">
<h1>Login Here</h1>
<section class="studentloginloginform cf">
<%= form_for :users,:url => {:action => 'loginuser',:controller => 'sessions'} do |f| %>
<% if @users.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@users.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @users.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<ul>
<li>
<label for="usermail">Email</label>
<%= f.email_field :email,placeholder:"yourname@email.com" %>
</li>
<li>
<label for="password">Password</label>
<%= f.password_field :password,placeholder:"password" %>
</li>
<li class="keeplogin">
<input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" />
<label for="loginkeeping">Keep me logged in</label>
</li>
<li>
<%= f.submit 'LogIn',:class => 'studentsubmit' %>
</li>
<li class="reg_member">
Not a member ? <%= link_to 'Register Here',homes_registration_path %>
</li>
<li>
<%= link_to 'Forget your password',homes_forgetpass_path %>
</li>
</ul>
<% end %>
</section>
</div>
</center>
</div>
</div>
<% end %>
controller/sessions_controller.rb
class SessionsController < ApplicationController
def loginuser
@users=User.authenticate(params[:users][:email], params[:users][:password])
if @users
session[:user_id]=@users.id
cookies.signed[:user_id]=@users.id
flash[:notice]="login successfull"
flash[:color]="valid"
redirect_to :action => 'member',:controller => 'homes'
else
flash[:notice]="could not Logged in"
flash[:color]="invalid"
redirect_to :action => 'member',:controller => 'homes'
end
end
def removeuser
session[:user_id] = nil
cookies.delete :user_id
flash[:notice]="user logged out successfully"
flash[:color]="valid"
redirect_to :action => 'member', :controller => 'homes'
end
end
As i am new to RoR please help me to resolve this issue.
Aucun commentaire:
Enregistrer un commentaire