Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
Venshop.v2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vy Quoc Vu
Venshop.v2
Commits
51bdd56c
Commit
51bdd56c
authored
Jul 28, 2015
by
Vy Quoc Vu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cart
parent
0e984652
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
84 additions
and
17 deletions
+84
-17
app/controllers/cart_products_controller.rb
+1
-0
app/controllers/carts_controller.rb
+38
-5
app/controllers/products_controller.rb
+2
-1
app/models/cart.rb
+5
-0
app/models/user.rb
+1
-0
app/views/cart_products/_new.html.erb
+5
-1
app/views/cart_products/index.html.erb
+5
-1
app/views/carts/info.html.erb
+6
-7
app/views/carts/show.html.erb
+18
-0
config/routes.rb
+3
-2
No files found.
app/controllers/cart_products_controller.rb
View file @
51bdd56c
class
CartProductsController
<
ApplicationController
class
CartProductsController
<
ApplicationController
before_action
:call_category
,
only:
[
:new
,
:index
]
before_action
:call_category
,
only:
[
:new
,
:index
]
include
CategoriesHelper
include
CategoriesHelper
def
add
def
add
id
=
params
[
:id
]
id
=
params
[
:id
]
if
session
[
:cart
]
then
if
session
[
:cart
]
then
...
...
app/controllers/carts_controller.rb
View file @
51bdd56c
class
CartsController
<
ApplicationController
class
CartsController
<
ApplicationController
include
CategoriesHelper
include
CategoriesHelper
def
show
def
show
if
user_signed_in?
@show_cart
=
Cart
.
where
(
user_id:
current_user
.
id
)
end
end
end
def
create
def
create
if
!
session
[
:cart
].
nil?
new_cart
=
Cart
.
new
(
cart_params
)
new_cart
=
Cart
.
new
(
cart_params
)
new_cart
.
total_price
=
calculate_total_price
new_cart
.
status
=
"In process"
if
user_signed_in?
new_cart
.
user_id
=
current_user
.
id
end
new_cart
.
save
new_cart
.
save
byebug
byebug
session
[
:cart
].
each
do
|
id
,
quantity
|
cart_product
=
CartProduct
.
new
cart_product
.
cart_id
=
new_cart
.
id
cart_product
.
product_id
=
id
cart_product
.
price
=
Product
.
find_by_id
(
id
).
price
cart_product
.
quantity
=
quantity
cart_product
.
save
end
end
flash
[
:success
]
=
"Success!"
def
info
session
[
:cart
]
=
nil
@cart
=
Cart
.
new
end
redirect_to
:action
=>
"show"
end
end
def
user_params
def
info
params
.
require
(
:cart
).
permit
(
:name
,
:email
,
:address
)
end
end
def
cart_params
def
cart_params
params
.
require
(
:session
).
permit
(
:name
,
:mail
,
:address
)
params
.
require
(
:session
).
permit
(
:name
,
:mail
,
:address
)
end
end
private
def
calculate_total_price
total
=
0
if
!
session
[
:cart
].
nil?
session
[
:cart
].
each
do
|
id
,
quantity
|
product
=
Product
.
find_by_id
(
id
)
if
!
product
.
nil?
total
=
total
+
product
.
price
*
quantity
end
end
end
total
end
end
end
app/controllers/products_controller.rb
View file @
51bdd56c
class
ProductsController
<
ApplicationController
class
ProductsController
<
ApplicationController
include
CategoriesHelper
include
CategoriesHelper
def
index
def
index
@products
=
Product
.
paginate
(
page:
params
[
:page
],
:per_page
=>
50
)
@products
=
Product
.
paginate
(
page:
params
[
:page
],
:per_page
=>
50
)
end
end
def
show
def
show
...
...
app/models/cart.rb
View file @
51bdd56c
class
Cart
<
ActiveRecord
::
Base
class
Cart
<
ActiveRecord
::
Base
validates
:name
,
presence:
true
,
length:
{
maximum:
50
}
VALID_EMAIL_REGEX
=
/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates
:mail
,
presence:
true
,
length:
{
maximum:
255
},
format:
{
with:
VALID_EMAIL_REGEX
}
belongs_to
:user
has_many
:cart_product
has_many
:cart_product
end
end
app/models/user.rb
View file @
51bdd56c
class
User
<
ActiveRecord
::
Base
class
User
<
ActiveRecord
::
Base
has_many
:carts
# Include default devise modules. Others available are:
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
# :confirmable, :lockable, :timeoutable and :omniauthable
devise
:database_authenticatable
,
:registerable
,
devise
:database_authenticatable
,
:registerable
,
...
...
app/views/cart_products/_new.html.erb
View file @
51bdd56c
<h1>
Cart Product
</h1>
<h1>
Cart Product
s
</h1>
<ul>
<ul>
<%
total
=
0
%>
<%
total
=
0
%>
<%
if
!
cart_product
.
nil?
%>
<%
cart_product
.
each
do
|
id
,
quantity
|
%>
<%
cart_product
.
each
do
|
id
,
quantity
|
%>
<%
product
=
Product
.
find_by_id
(
id
)
%>
<%
product
=
Product
.
find_by_id
(
id
)
%>
<%
if
!
product
.
nil?
%>
<%
if
!
product
.
nil?
%>
...
@@ -10,7 +11,10 @@
...
@@ -10,7 +11,10 @@
<%=
link_to
truncate
(
product
.
name
,
length
:
15
),
"/products/
#{
product
.
id
}
"
%>
<%=
link_to
truncate
(
product
.
name
,
length
:
15
),
"/products/
#{
product
.
id
}
"
%>
<%=
quantity
%>
<%=
quantity
%>
</li>
</li>
<%
end
%>
<%
end
-%>
<%
end
-%>
<%
else
%>
<h3>
Empty
</h3>
<%
end
-%>
<%
end
-%>
<br>
<br>
<br>
<br>
...
...
app/views/cart_products/index.html.erb
View file @
51bdd56c
<h1>
Your Cart
</h1>
<h1>
Your Cart
</h1>
<%=
link_to
"Empty Cart"
,
cart_products_clear_path
%>
<%=
link_to
"Empty Cart"
,
'/cart_products/clear'
%>
<ul>
<ul>
<%
total
=
0
%>
<%
total
=
0
%>
<%
if
!
@cart_product
.
nil?
%>
<%
@cart_product
.
each
do
|
id
,
quantity
|
%>
<%
@cart_product
.
each
do
|
id
,
quantity
|
%>
<%
product
=
Product
.
find_by_id
(
id
)
%>
<%
product
=
Product
.
find_by_id
(
id
)
%>
<%
if
!
product
.
nil?
%>
<%
if
!
product
.
nil?
%>
...
@@ -13,6 +14,9 @@
...
@@ -13,6 +14,9 @@
</div>
</div>
<%
end
%>
<%
end
%>
<%
end
-%>
<%
end
-%>
<%
else
%>
<h3>
Empty
</h3>
<%
end
-%>
</ul>
</ul>
<div
class=
"col-md-12"
>
<div
class=
"col-md-12"
>
<nav>
<nav>
...
...
app/views/carts/info.html.erb
View file @
51bdd56c
<h1>
Cart info
</h1>
<h1>
Cart info
</h1>
<ul>
<ul>
<%
total
=
0
%>
<%
@
total
=
0
%>
<%
cart_product
.
each
do
|
id
,
quantity
|
%>
<%
cart_product
.
each
do
|
id
,
quantity
|
%>
<%
product
=
Product
.
find_by_id
(
id
)
%>
<%
product
=
Product
.
find_by_id
(
id
)
%>
<%
if
!
product
.
nil?
%>
<%
if
!
product
.
nil?
%>
<%
total
=
total
+
product
.
price
*
quantity
%>
<%
@total
=
@
total
+
product
.
price
*
quantity
%>
<li>
<li>
<%=
link_to
product
.
name
,
"/products/
#{
product
.
id
}
"
%>
<%=
link_to
product
.
name
,
"/products/
#{
product
.
id
}
"
%>
</br>
</br>
...
@@ -13,22 +13,21 @@
...
@@ -13,22 +13,21 @@
<%
end
-%>
<%
end
-%>
<%
end
-%>
<%
end
-%>
<br>
<br>
<h2>
<%=
number_to_currency
(
total
/
100
.
to_f
,
:unit
=>
'$'
)
%>
</h2>
<h2>
<%=
number_to_currency
(
@
total
/
100
.
to_f
,
:unit
=>
'$'
)
%>
</h2>
</ul>
</ul>
<div
class=
"row"
>
<div
class=
"row"
>
<div
class=
"col-md-10 "
>
<div
class=
"col-md-10 "
>
<%=
form_for
(
:session
,
url:
create_cart_path
)
do
|
f
|
%>
<%=
form_for
(
:session
,
url:
create_cart_path
)
do
|
f
|
%>
<%=
f
.
label
:
e
mail
%>
<%=
f
.
label
:mail
%>
<%=
f
.
email_field
:
e
mail
,
class:
'form-control'
%>
<%=
f
.
email_field
:mail
,
class:
'form-control'
%>
<%=
f
.
label
:name
%>
<%=
f
.
label
:name
%>
<%=
f
.
text_field
:name
,
class:
'form-control'
%>
<%=
f
.
text_field
:name
,
class:
'form-control'
%>
<%=
f
.
label
:address
%>
<%=
f
.
label
:address
%>
<%=
f
.
text_field
:address
,
class:
'form-control'
%>
<%=
f
.
text_field
:address
,
class:
'form-control'
%>
</br>
</br>
<%=
f
.
submit
"
Log in"
,
class:
"btn btn-primary"
%>
<%=
f
.
submit
"
Submit"
,
class:
"btn btn-primary"
%>
<%
end
%>
<%
end
%>
</div>
</div>
</div>
</div>
app/views/carts/show.html.erb
0 → 100644
View file @
51bdd56c
<h1>
show cart
</h1>
<ul>
<%
if
!
@show_cart
.
nil?
%>
<%
@show_cart
.
each
do
|
cart
|
%>
<h4>
<%=
cart
.
id
.
to_s
+
" | "
%>
<%=
cart
.
mail
.
to_s
+
" | "
%>
<%=
cart
.
name
.
to_s
+
" | "
%>
<%=
cart
.
total_price
.
to_s
+
" | "
%>
<%=
cart
.
status
.
to_s
+
" | "
%>
<%=
cart
.
address
%>
</h4>
<%
end
-%>
<%
else
-%>
<h2>
Your cart Empty
</h2>
<%
end
-%>
</ul>
config/routes.rb
View file @
51bdd56c
...
@@ -4,17 +4,18 @@
...
@@ -4,17 +4,18 @@
get
'login'
=>
'sessions#new'
get
'login'
=>
'sessions#new'
get
'category/:id'
=>
'category#show'
get
'category/:id'
=>
'category#show'
get
'
categories/
products/:id'
=>
'products#show'
get
'products/:id'
=>
'products#show'
get
'cart_products/:id'
=>
'cart_products#add'
get
'cart_products/:id'
=>
'cart_products#add'
get
'cart_products/clear'
=>
'cart_products#clear_cart_product'
get
'cart_products/clear'
=>
'cart_products#clear_cart_product'
get
'cart'
=>
'carts#info'
get
'cart'
=>
'carts#info'
get
'carts'
=>
'carts#new'
get
'carts'
=>
'carts#new'
get
'carts/show'
=>
'carts#show'
post
'carts/create'
=>
'carts#create'
,
as:
'create_cart'
post
'carts/create'
=>
'carts#create'
,
as:
'create_cart'
resources
:categories
,
only:
[
:index
,
:show
]
resources
:categories
,
only:
[
:index
,
:show
]
resources
:products
,
only:
[
:index
,
:show
]
resources
:products
,
only:
[
:index
,
:show
]
resources
:carts
,
only:
[
:info
]
resources
:carts
,
only:
[
:info
,
:destroy
]
resources
:cart_products
,
only:
[
:new
,
:edit
,
:update
,
:index
]
resources
:cart_products
,
only:
[
:new
,
:edit
,
:update
,
:index
]
root
"products#index"
root
"products#index"
# get 'cate/:id' => 'cacate#show'
# get 'cate/:id' => 'cacate#show'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment