Commit 7546f5b7 by vulehuan

User login, log out, update profile function

parent 09930b1b
(function($) {
$(function() {
// message
if ($('#block-message').length && $('#block-message-visible').length) {
$('#block-message-visible').removeClass('hidden');
$('#block-message-visible').html($('#block-message').html());
}
//
$('.carousel').carousel();
});
})(jQuery);
jQuery.noConflict();
\ No newline at end of file
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
...@@ -14,13 +14,35 @@ $screen-desktop: $screen-md; ...@@ -14,13 +14,35 @@ $screen-desktop: $screen-md;
$screen-lg: 1200px; $screen-lg: 1200px;
$screen-lg-desktop: $screen-lg; $screen-lg-desktop: $screen-lg;
@font-face {
font-family: 'OpenSansRegular';
src: url('../fonts/opensansregular.eot');
src: url('../fonts/opensansregular.eot') format('embedded-opentype'),
url('../fonts/opensansregular.woff') format('woff'),
url('../fonts/opensansregular.ttf') format('truetype'),
url('../fonts/opensansregular.svg#OpenSansRegular') format('svg');
}
@font-face {
font-family: 'TwCenMTRegular';
src: url('../fonts/tcm.eot');
src: url('../fonts/tcm.eot') format('embedded-opentype'),
url('../fonts/tcm.woff') format('woff'),
url('../fonts/tcm.ttf') format('truetype'),
url('../fonts/tcm.svg#TwCenMTRegular') format('svg');
}
body { body {
font-family: Tahoma, "Times New Roman", Times, serif; font-family: OpenSansRegular, Tahoma, "Times New Roman", Times, serif;
font-size: 12px; font-size: 12px;
color: #000; color: #000;
background-image: url("../images/bg1.jpg"); background-image: url("../images/bg1.jpg");
} }
h1, h2, h3, h4, h5, h6 {
font-family: 'TwCenMTRegular';
}
.container { .container {
background: #FFF; background: #FFF;
} }
...@@ -37,7 +59,17 @@ body { ...@@ -37,7 +59,17 @@ body {
margin-top: 10px; margin-top: 10px;
} }
.text-justify {
text-align: justify;
padding: 10px 0;
}
.btn-submit {
margin: 5px 0;
}
header { header {
font-family: 'TwCenMTRegular';
padding: 5px 0; padding: 5px 0;
margin-right: -15px; margin-right: -15px;
@media (max-width: $screen-xs) { @media (max-width: $screen-xs) {
...@@ -189,7 +221,6 @@ h1 { ...@@ -189,7 +221,6 @@ h1 {
} }
} }
a { a {
font-weight: bold;
color: #000; color: #000;
&:hover { &:hover {
color: #900; color: #900;
...@@ -295,11 +326,11 @@ footer { ...@@ -295,11 +326,11 @@ footer {
} }
.price { .price {
color: #000; color: #000;
font-weight: bold;
text-align: center; text-align: center;
padding-top: 15px; padding-top: 15px;
padding-bottom: 5px; padding-bottom: 5px;
span { span {
font-weight: bold;
color: #F00; color: #F00;
} }
} }
...@@ -390,3 +421,13 @@ footer { ...@@ -390,3 +421,13 @@ footer {
margin: 5px; margin: 5px;
margin-top: 0; margin-top: 0;
} }
.alert {
.alert {
padding-bottom: 0;
}
}
.alert-error {
color: #b94a48;
background-color: #f2dede;
border-color: #eed3d7;
}
\ No newline at end of file
// Place all the styles related to the Sessions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
...@@ -2,4 +2,5 @@ class ApplicationController < ActionController::Base ...@@ -2,4 +2,5 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead. # For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception protect_from_forgery with: :exception
include SessionsHelper
end end
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
sign_out
redirect_to root_url
end
end
class UsersController < ApplicationController class UsersController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
@users = User.paginate(page: params[:page])
end
def new def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
@user = User.new(user_params)
if @user.save
sign_in @user
flash[:success] = "Welcome, #{@user.name}!"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted."
redirect_to users_url
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
# Before filters
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end end
end end
\ No newline at end of file
module SessionsHelper
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
def current_user?(user)
user == current_user
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
session.delete(:return_to)
end
def store_location
session[:return_to] = request.url if request.get?
end
end
module UsersHelper module UsersHelper
# Returns the Gravatar (http://gravatar.com) for the given user.
def gravatar_for(user, options = { size: 50, out_put_img: true })
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
if options[:out_put_img]
return image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
return gravatar_url
end
end end
\ No newline at end of file
class User < ActiveRecord::Base class User < ActiveRecord::Base
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.encrypt(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.encrypt(User.new_remember_token)
end
end end
\ No newline at end of file
<header> <header>
<div class="row"> <div class="row">
<div class="col-md-2 col-sm-2"> <div class="col-md-2 col-sm-2">
<h1 class="logo pull-left"><a href="">Venshop</a></h1> <h1 class="logo pull-left"><a href="<%= root_path %>">Venshop</a></h1>
</div> </div>
<div class="col-md-10 col-sm-10"> <div class="col-md-10 col-sm-10">
<div class="banner pull-right"> <div class="banner pull-right">
<a href=""><img src="<%= image_path('') %>/images/upload/banner/1.png" class="img-responsive" alt="" /></a> <a href="<%= root_path %>"><img src="<%= image_path('') %>/images/upload/banner/1.png" class="img-responsive" alt="" /></a>
</div> </div>
</div> </div>
</div> </div>
<div class="block-user-action"> <div class="block-user-action">
<div class="btn-group"> <div class="btn-group">
<a data-toggle="modal" href="#map-modal" class="btn btn-primary"> <% if signed_in? %>
<span class="glyphicon glyphicon-user"></span> Login <%= link_to edit_user_path(current_user), class: "btn btn-default" do %>
</a> <a href="" class="btn btn-default"> <span <span class="glyphicon glyphicon-edit"></span> Settings
class="glyphicon glyphicon-hand-right"></span> Register <% end %>
</a> <a href="" class="btn btn-danger last"> <span <a href="" class="btn btn-danger last"> <span class="glyphicon glyphicon-shopping-cart"></span> 0 item(s)
class="glyphicon glyphicon-shopping-cart"></span> 0 item(s)
</a> </a>
<%= link_to signout_path, class: "btn btn-primary", method: "delete", dada: { confirm: "Do you want to logout?" } do %>
<span class="glyphicon glyphicon-log-out"></span> Logout
<% end %>
<% else %>
<%= link_to signin_path, class: "btn btn-primary" do %>
<span class="glyphicon glyphicon-log-in"></span> Login
<% end %>
<%= link_to signup_path, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-user"></span> Register
<% end %>
<a href="" class="btn btn-danger last"> <span class="glyphicon glyphicon-shopping-cart"></span> 0 item(s)
</a>
<% end %>
</div> </div>
</div> </div>
<div class="clearfix"></div> <div class="clearfix"></div>
......
...@@ -36,9 +36,13 @@ ...@@ -36,9 +36,13 @@
</ul> </ul>
</nav> </nav>
<div class="clearfix"></div> <div class="clearfix"></div>
<% if flash.any? %>
<div id="block-message" class="hidden">
<% flash.each do |key, value| %> <% flash.each do |key, value| %>
<%= content_tag(:div, value, class: "alert alert-#{key}") %> <%= content_tag(:div, value, class: "alert alert-#{key}") %>
<% end %> <% end %>
</div>
<% end %>
<%= yield %> <%= yield %>
</div> </div>
</div> </div>
......
<% provide(:title, "Sign in") %>
<div class="body-box">
<h2 class="sprite-2">Sign in<span class="sprite-2"></span></h2>
<div class="text-justify">
<div id="block-message-visible" class="hidden"></div>
<%= form_for(:session, url: sessions_path) do |f| %>
<%= f.label :email %>
<%= f.text_field :email, class: "form-control" %>
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
<%= f.submit "Sign in", class: "btn btn-large btn-primary btn-submit" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>
\ No newline at end of file
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@user.errors.count, "error") %>.
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
\ No newline at end of file
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: "form-control" %>
<%= f.label :email %>
<%= f.text_field :email, class: "form-control", class: "form-control" %>
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation, class: "form-control" %>
\ No newline at end of file
<% provide(:title, "Edit user") %>
<div class="body-box">
<h2 class="sprite-2">Update your profile<span class="sprite-2"></span></h2>
<div class="text-justify">
<%= form_for(@user) do |f| %>
<%= render 'fields', f:f %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary btn-submit" %>
<% end %>
<%= gravatar_for @user %>
<a href="http://gravatar.com/emails">change</a>
</div>
</div>
\ No newline at end of file
<h1>Users#new</h1> <% provide(:title, 'Sign up') %>
<p>Find me in app/views/users/new.html.erb</p> <div class="body-box">
<h2 class="sprite-2">Sign up<span class="sprite-2"></span></h2>
<div class="text-justify">
<%= form_for(@user) do |f| %>
<%= render 'fields', f:f %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary btn-submit" %>
<% end %>
</div>
</div>
\ No newline at end of file
<% provide(:title, @user.name) %>
<div class="body-box">
<h2 class="sprite-2">User<span class="sprite-2"></span></h2>
<div class="text-justify">
<ul class="media-list">
<li class="media">
<a class="pull-left" href="javascript: void(0)">
<img class="media-object" src="<%= gravatar_for(@user, { size: 200, out_put_img: false }) %>" alt="<%= @user.name %>" /></a>
<div class="media-body">
<h4 class="media-heading"><%= @user.name %></h4>
<b>Email:</b> <a href="mailto:<%= @user.email %>"><%= @user.email %></a>
</div>
</li>
</div>
</div>
\ No newline at end of file
VenshopApp::Application.routes.draw do VenshopApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
get "default_pages/home" get "default_pages/home"
get "users/new" get "users/new"
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
# The priority is based upon order of creation: first created -> highest priority. # The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes". # See how all your routes lay out with "rake routes".
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment