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
before_action
:call_category
,
only:
[
:new
,
:index
]
include
CategoriesHelper
def
add
id
=
params
[
:id
]
if
session
[
:cart
]
then
...
...
app/controllers/carts_controller.rb
View file @
51bdd56c
class
CartsController
<
ApplicationController
include
CategoriesHelper
def
show
if
user_signed_in?
@show_cart
=
Cart
.
where
(
user_id:
current_user
.
id
)
end
end
def
create
if
!
session
[
:cart
].
nil?
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
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
def
info
@cart
=
Cart
.
new
flash
[
:success
]
=
"Success!"
session
[
:cart
]
=
nil
end
redirect_to
:action
=>
"show"
end
def
user_params
params
.
require
(
:cart
).
permit
(
:name
,
:email
,
:address
)
def
info
end
def
cart_params
params
.
require
(
:session
).
permit
(
:name
,
:mail
,
:address
)
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
app/controllers/products_controller.rb
View file @
51bdd56c
class
ProductsController
<
ApplicationController
include
CategoriesHelper
def
index
@products
=
Product
.
paginate
(
page:
params
[
:page
],
:per_page
=>
50
)
end
def
show
...
...
app/models/cart.rb
View file @
51bdd56c
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
end
app/models/user.rb
View file @
51bdd56c
class
User
<
ActiveRecord
::
Base
has_many
:carts
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise
:database_authenticatable
,
:registerable
,
...
...
app/views/cart_products/_new.html.erb
View file @
51bdd56c
<h1>
Cart Product
</h1>
<h1>
Cart Product
s
</h1>
<ul>
<%
total
=
0
%>
<%
if
!
cart_product
.
nil?
%>
<%
cart_product
.
each
do
|
id
,
quantity
|
%>
<%
product
=
Product
.
find_by_id
(
id
)
%>
<%
if
!
product
.
nil?
%>
...
...
@@ -10,7 +11,10 @@
<%=
link_to
truncate
(
product
.
name
,
length
:
15
),
"/products/
#{
product
.
id
}
"
%>
<%=
quantity
%>
</li>
<%
end
%>
<%
end
-%>
<%
else
%>
<h3>
Empty
</h3>
<%
end
-%>
<br>
<br>
...
...
app/views/cart_products/index.html.erb
View file @
51bdd56c
<h1>
Your Cart
</h1>
<%=
link_to
"Empty Cart"
,
cart_products_clear_path
%>
<%=
link_to
"Empty Cart"
,
'/cart_products/clear'
%>
<ul>
<%
total
=
0
%>
<%
if
!
@cart_product
.
nil?
%>
<%
@cart_product
.
each
do
|
id
,
quantity
|
%>
<%
product
=
Product
.
find_by_id
(
id
)
%>
<%
if
!
product
.
nil?
%>
...
...
@@ -13,6 +14,9 @@
</div>
<%
end
%>
<%
end
-%>
<%
else
%>
<h3>
Empty
</h3>
<%
end
-%>
</ul>
<div
class=
"col-md-12"
>
<nav>
...
...
app/views/carts/info.html.erb
View file @
51bdd56c
<h1>
Cart info
</h1>
<ul>
<%
total
=
0
%>
<%
@
total
=
0
%>
<%
cart_product
.
each
do
|
id
,
quantity
|
%>
<%
product
=
Product
.
find_by_id
(
id
)
%>
<%
if
!
product
.
nil?
%>
<%
total
=
total
+
product
.
price
*
quantity
%>
<%
@total
=
@
total
+
product
.
price
*
quantity
%>
<li>
<%=
link_to
product
.
name
,
"/products/
#{
product
.
id
}
"
%>
</br>
...
...
@@ -13,22 +13,21 @@
<%
end
-%>
<%
end
-%>
<br>
<h2>
<%=
number_to_currency
(
total
/
100
.
to_f
,
:unit
=>
'$'
)
%>
</h2>
<h2>
<%=
number_to_currency
(
@
total
/
100
.
to_f
,
:unit
=>
'$'
)
%>
</h2>
</ul>
<div
class=
"row"
>
<div
class=
"col-md-10 "
>
<%=
form_for
(
:session
,
url:
create_cart_path
)
do
|
f
|
%>
<%=
f
.
label
:
e
mail
%>
<%=
f
.
email_field
:
e
mail
,
class:
'form-control'
%>
<%=
f
.
label
:mail
%>
<%=
f
.
email_field
:mail
,
class:
'form-control'
%>
<%=
f
.
label
:name
%>
<%=
f
.
text_field
:name
,
class:
'form-control'
%>
<%=
f
.
label
:address
%>
<%=
f
.
text_field
:address
,
class:
'form-control'
%>
</br>
<%=
f
.
submit
"
Log in"
,
class:
"btn btn-primary"
%>
<%=
f
.
submit
"
Submit"
,
class:
"btn btn-primary"
%>
<%
end
%>
</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 @@
get
'login'
=>
'sessions#new'
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/clear'
=>
'cart_products#clear_cart_product'
get
'cart'
=>
'carts#info'
get
'carts'
=>
'carts#new'
get
'carts/show'
=>
'carts#show'
post
'carts/create'
=>
'carts#create'
,
as:
'create_cart'
resources
:categories
,
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
]
root
"products#index"
# 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