Commit e0aa1263 by nnnghia98

create js file for fav button

parent e5cbddc7
class FavoritesController < ApplicationController class FavoritesController < ApplicationController
before_action :authenticate_user!, only: [:create, :destroy] before_action :authenticate_user!, only: [:create, :destroy]
before_action :find_favorited_job, only: [:create, :destroy]
def create def create
redirect_to new_user_session_path unless user_signed_in? 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] job_id = @job.id
@all_favorite_job.find_by(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) @all_favorite_job.create!(user_id: current_user.id, job_id: job_id, favorited_at: Time.current)
redirect_to job_path(job_id)
end end
def destroy def destroy
redirect_to new_user_session_path unless user_signed_in? 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] redirect_to job_path(job_id)
@all_favorite_job.find_by(job_id: @job_id).destroy
end end
private private
......
$("#favorite_form_<%= @job.id %>").html("<%= escape_javascript(render "shared/unfavorite_btn", job: @job %>");
$("#favorite_form_<%= @job.id %>").html("<%= escape_javascript(render "shared/favorite_btn", job: @job) %>");
...@@ -17,5 +17,12 @@ ...@@ -17,5 +17,12 @@
<dd><%= job["city"] %></dd> <dd><%= job["city"] %></dd>
</dl> </dl>
</div> </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> </div>
<% if user_signed_in? %> <% 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 %> <% else %>
<button type="button" class="btn btn-primary float-right" disabled>Favorite</button> <button type="button" class="btn btn-primary float-right" disabled>Favorite</button>
<% end %> <% end %>
<% if user_signed_in? %> <% 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 %> <% else %>
<button type="button" class="btn btn-primary float-right" disabled>Unfavorite</button> <button type="button" class="btn btn-primary float-right" disabled>Unfavorite</button>
<% end %> <% end %>
...@@ -24,4 +24,8 @@ Rails.application.routes.draw do ...@@ -24,4 +24,8 @@ Rails.application.routes.draw do
resource :favorites, only: [:create, :destroy] resource :favorites, only: [:create, :destroy]
# post "favorite", to: "favorites#create" # post "favorite", to: "favorites#create"
# delete "unfavorite", to: "favorites#destroy" # delete "unfavorite", to: "favorites#destroy"
resource :jobs do
resource :favorites, only: [:create, :destroy]
end
end end
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