Commit 4c4659ae by Hoang Nam Nguyen

'create favoritepage'

parent be5a251e
class FavoriteController < ApplicationController
before_action :logged_in?
def create
binding.pry
@job = Job.find(params[:favorite_job])
current_user.favorite!(@job)
end
def destroy
@job = Favorite.find(params[:id]).job
current_user.unfavorite!(@job)
end
# private
# def set_job_id
# session[:job] = Job.find(params[:favorite_job][:job_id])
# end
end
......@@ -51,6 +51,11 @@ class UsersController < ApplicationController
@job_pages= ::Kaminari.paginate_array(@user.applies).page(params[:page]).per(20)
end
def favorite
@title = 'Favorite Jobs'
@job = current_user.jobs.build
@feed_jobs = current_user.favorite_jobs.paginate(page: params[:page])
end
private
def user_params
......
module FavoriteHelper
end
class Favorite < ApplicationRecord
belongs_to :user
belongs_to :job
end
......@@ -6,6 +6,8 @@ class Job < ApplicationRecord
belongs_to :company
has_many :applies
has_many :histories
has_many :favorites
has_many :favoriting_users, through: :favorites,source: :user
scope :recent, -> (num) { order(id: :desc).limit(num) }
end
class User < ApplicationRecord
has_many :applies
has_many :histories
has_many :favorites
has_many :favorite_jobs, through: :favorites,source: :job
has_secure_password(validations: false)
......@@ -54,6 +56,18 @@ class User < ApplicationRecord
def applied_job?(job)
applies.pluck(:job_id).include?(job.id)
end
def favorite?(job)
favorites.find_by(job_id: job['job_id'])
end
def favorite!(job)
favorites.create!(job_id: job.id)
end
def unfavorites!(job)
favorites.find_by(job_id: job.id).destroy
end
private
def downcase_email
......
<%= form_for(:favorite_job, url: create_favorite_path, remote: true, method: :post) do |f| %>
<div><%= f.hidden_field :job_id, value: job['job_id'] %></div>
<%= f.submit "Favorite",class: 'fa fa-heartbeat' %>
<% end %>
\ No newline at end of file
<%= form_for(current_user.favorites.find_by(job_id: job['job_id']),
html: {method: :delete}) do |f| %>
<%= f.submit "Unfavorite", class: "btn"%>
<% end %>
\ No newline at end of file
// $("#favorite_form").html("<%= escape_javascript(render('users/unfavorite')) %>");
// $("#favorites").html('<%= current_.favorites.job.count %>');
$("#follow_form").html('<%= escape_javascript(render("favorites/create", job: @job)) %>')
$('#favorites').text('<%= current_user.favorites.count %>')
\ No newline at end of file
// $("#follow_form").html("<%= escape_javascript(render('favorite/favorite')) %>");
// $("#favorites").html('<%= current_user.favorites.job.count %>');
$('#follow_form %>').html('<%= escape_javascript(render("favorite/destroy", job: @job)) %>');
$('#favorites').text('<%= current_user.favorites.count %>');
\ No newline at end of file
......@@ -22,10 +22,13 @@
</div>
<div class="col-md-2 mt-2">
<%= link_to '',class: 'btn btn-success' do %>
<i class="fa fa-heartbeat"></i> Favorite
<div id="follow_form">
<% if current_user.favorite?(job) %>
<%= render 'favorite/destroy', job: job %>
<% else %>
<%= render 'favorite/create', job: job %>
<% end %>
</div>
</div>
</div>
</div>
\ No newline at end of file
<% @user ||= current_user %>
<div class="stats">
<a href="<%= following_user_path(@user) %>">
<strong id="following" class="stat">
<%= @user.followings.count %>
</strong>
following
</a>
<a href="<%= followers_user_path(@user) %>">
<strong id="followers" class="stat">
<%= @user.followers.count %>
</strong>
followers
</a>
<a href="<%= favorite_user_path(@user) %>">
<strong id="favorites" class="stat">
<%= @user.favorite_jobs.count %>
</strong>
favorites
</a>
</div>
\ No newline at end of file
<%= form_for(:favorite_job, url: create_favorite_path, remote: true, method: :post) do |f| %>
<div><%= f.hidden_field :job_id, value: job['job_id'] %></div>
<%= f.submit "Favorite",class: 'fa fa-heartbeat' %>
<% end %>
\ No newline at end of file
......@@ -15,6 +15,7 @@ module VenJob
require 'carrierwave'
require 'carrierwave/orm/activerecord'
config.time_zone = 'Hanoi'
config.action_view.embed_authenticity_token_in_remote_forms = true
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
......
......@@ -8,7 +8,7 @@ Rails.application.routes.draw do
get '/cities',to: 'top_pages#cities'
get '/industries',to: 'top_pages#industries'
get '/history',to: 'top_pages#history'
get '/favorite',to: 'top_pages#favorite_job'
get 'top_pages/cities'
get 'top_pages/industries'
get 'top_pages/top_page'
......@@ -34,5 +34,15 @@ Rails.application.routes.draw do
post 'apply',controller: :applies,action: :create,as: :new_info
get 'apply/confirm', controller: :applies,action: :confirm_apply,as: :confirm_apply
get 'apply/finish',controller: :applies,action: :finish,as: :finish_apply
get '/favorites',controller: :favorite,action: :favorite_list,as: :favorite_jobs
post '/favorites',controller: :favorite,action: :create,as: :create_favorite
resources :users do
member do
get :following, :followers, :favorite
end
end
# resources :favorite, only: [:create, :destroy]
mount Sidekiq::Web => '/sidekiq'
end
class CreateFavorites < ActiveRecord::Migration[5.1]
def change
create_table :favorites do |t|
t.integer :user_id
t.integer :job_id
t.timestamps
end
add_index :favorites, :user_id
add_index :favorites, :job_id
add_index :favorites, [:user_id, :job_id], unique: true
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20171005062654) do
ActiveRecord::Schema.define(version: 20171006073026) do
create_table "add_cv_to_users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "cv"
......@@ -43,6 +43,13 @@ ActiveRecord::Schema.define(version: 20171005062654) do
t.datetime "updated_at", null: false
end
create_table "favorites", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "user_id"
t.integer "job_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "histories", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "job_id"
t.datetime "created_at", null: false
......
require 'test_helper'
class FavoriteControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
user_id: 1
job_id: 1
two:
user_id: 1
job_id: 1
require 'test_helper'
class FavoriteTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# 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