Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
VeNJOB
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
Nguyen Ngoc Nghia
VeNJOB
Commits
e0aa1263
Commit
e0aa1263
authored
Mar 02, 2020
by
nnnghia98
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
create js file for fav button
parent
e5cbddc7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
32 additions
and
10 deletions
+32
-10
app/controllers/favorites_controller.rb
+12
-7
app/views/favorites/create.js.erb
+1
-0
app/views/favorites/destroy.js.erb
+1
-0
app/views/jobs/_job.html.erb
+8
-1
app/views/shared/_favorite_btn.html.erb
+3
-1
app/views/shared/_unfavorite_btn.html.erb
+3
-1
config/routes.rb
+4
-0
No files found.
app/controllers/favorites_controller.rb
View file @
e0aa1263
class
FavoritesController
<
ApplicationController
before_action
:authenticate_user!
,
only:
[
:create
,
:destroy
]
before_action
:find_favorited_job
,
only:
[
:create
,
:destroy
]
def
create
redirect_to
new_user_session_path
unless
user_signed_in?
redirect_to
jobs_path
if
params
[
:job_id
]
.
blank?
redirect_to
jobs_path
if
@job
.
id
.
blank?
@job_id
=
params
[
:job_id
]
@all_favorite_job
.
find_by
(
job_id:
@job_id
)
||
@all_favorite_job
.
create!
(
user_id:
current_user
.
id
,
job_id:
@job_id
,
favorited_at:
Time
.
current
)
job_id
=
@job
.
id
@all_favorite_job
.
find_by
(
job_id:
job_id
)
||
@all_favorite_job
.
create!
(
user_id:
current_user
.
id
,
job_id:
job_id
,
favorited_at:
Time
.
current
)
redirect_to
job_path
(
job_id
)
end
def
destroy
redirect_to
new_user_session_path
unless
user_signed_in?
redirect_to
jobs_path
if
params
[
:job_id
].
blank?
redirect_to
jobs_path
if
@job
.
id
.
blank?
job_id
=
@job
.
id
@all_favorite_job
.
find_by
(
job_id:
job_id
).
destroy
@job_id
=
params
[
:job_id
]
@all_favorite_job
.
find_by
(
job_id:
@job_id
).
destroy
redirect_to
job_path
(
job_id
)
end
private
...
...
app/views/favorites/create.js.erb
0 → 100644
View file @
e0aa1263
$("#favorite_form_
<%=
@job
.
id
%>
").html("
<%=
escape_javascript
(
render
"shared/unfavorite_btn"
,
job:
@job
%>
");
app/views/favorites/destroy.js.erb
0 → 100644
View file @
e0aa1263
$("#favorite_form_
<%=
@job
.
id
%>
").html("
<%=
escape_javascript
(
render
"shared/favorite_btn"
,
job:
@job
)
%>
");
app/views/jobs/_job.html.erb
View file @
e0aa1263
...
...
@@ -17,5 +17,12 @@
<dd>
<%=
job
[
"city"
]
%>
</dd>
</dl>
</div>
<button
type=
"button"
class=
"btn btn-outline-secondary"
>
Favorite
</button>
<div
id=
"favorite_form_"
>
<button
type=
"button"
class=
"btn btn-outline-secondary"
>
Favorite
</button>
<%
if
current_user
.
favorites
.
exists?
(
job_id:
job
.
id
)
%>
<%=
render
"shared/unfavorite_btn"
,
job_id:
job
.
id
%>
<%
else
%>
<%=
render
"shared/favorite_btn"
,
job_id:
job
.
id
%>
<%
end
%>
</div>
</div>
app/views/shared/_favorite_btn.html.erb
View file @
e0aa1263
<%
if
user_signed_in?
%>
<%=
link_to
"Favorite"
,
"#"
,
class:
"btn btn-primary float-right"
%>
<%=
form_tag
(
jobs_favorites_path
(
job_id:
@job
.
id
)
,
remote:
true
)
do
%>
<%=
submit_tag
"Favorite"
,
class:
"btn btn-primary float-right"
%>
<%
end
%>
<%
else
%>
<button
type=
"button"
class=
"btn btn-primary float-right"
disabled
>
Favorite
</button>
<%
end
%>
app/views/shared/_unfavorite_btn.html.erb
View file @
e0aa1263
<%
if
user_signed_in?
%>
<%=
link_to
"Unfavorite"
,
"#"
,
class:
"btn btn-primary float-right"
%>
<%=
form_tag
(
jobs_favorite_path
(
job_id:
@job
.
id
),
method: :delete
,
remote:
true
)
do
%>
<%=
submit_tag
"Unfavorite"
,
class:
"btn btn-primary float-right"
%>
<%
end
%>
<%
else
%>
<button
type=
"button"
class=
"btn btn-primary float-right"
disabled
>
Unfavorite
</button>
<%
end
%>
config/routes.rb
View file @
e0aa1263
...
...
@@ -24,4 +24,8 @@ Rails.application.routes.draw do
resource
:favorites
,
only:
[
:create
,
:destroy
]
# post "favorite", to: "favorites#create"
# delete "unfavorite", to: "favorites#destroy"
resource
:jobs
do
resource
:favorites
,
only:
[
:create
,
:destroy
]
end
end
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