Commit a0a457d1 by Đường Sỹ Hoàng

Fix bug model User, create Apply, apply process

parent 5afdb94c
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
added_attrs = [:remember_me, :username, :first_name, :last_name]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
end
def check_user_logged_in?
redirect_to login_path unless user_signed_in?
end
def after_sign_in_path_for(resource)
stored_location_for(resource) || my_path
end
protected
def configure_permitted_parameters
added_attrs = [:remember_me, :username, :first_name, :last_name]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
end
def after_sign_in_path_for(resource)
stored_location_for(resource) || my_path
end
end
class ApplyController < ApplicationController
before_action :check_user_logged_in?
before_action :load_job, only: [:new, :done, :confirm]
def new
@apply = if params[:apply].present?
current_user.applies.new(apply_job_params)
else
current_user.applies.new
end
end
def confirm
@apply = current_user.applies.new(apply_job_params)
end
def done
binding.pry
@apply = UserJob.new(apply_job_params)
@apply = current_user.applies.new(apply_job_params)
if @apply.save
redirect_to done_path
flash[:success] = "Your application has been saved successfully!"
else
render :new
end
end
def new
@apply = UserJob.new
end
def confirm
@apply = UserJob.new(apply_job_params)
end
private
def apply_job_params
params.require(:user_job).permit(:cv_url, :first_name, :last_name, :username, :email)
end
def load_job
@job = Job.find_by(id: params[:job_id])
end
def apply_job_params
params.require(:apply)
.permit(:cv_url, :first_name, :last_name, :username, :email)
.merge(job_id: @job.id)
end
end
class Apply < UserJob
end
class ApplyJob < UserJob
belongs_to :users
belongs_to :jobs
end
class Favorite < UserJob
end
class History < UserJob
end
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable
has_many :user_jobs
has_many :apply_jobs
has_many :applies
has_many :favorites
has_many :histories
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
VALID_USERNAME_REGEX = /\A[a-zA-Z0-9]+\z/
......
......@@ -22,7 +22,7 @@ Rails.application.routes.draw do
get "jobs/industry/:industry_id", to: "jobs#index", as: "industry_jobs"
get "detail/:job_id", to: "jobs#show", as: "job"
get "apply/:job_id", to: "apply#new", as: "apply"
post "confirm/:job_id/", to: "apply#confirm", as: "confirm"
post "confirm/:job_id", to: "apply#confirm", as: "confirm"
post "done/:job_id", to: "apply#done", as: "done"
concern :paginatable do
......
class ModifyUser < ActiveRecord::Migration[6.0]
def change
remove_column :users, :password_digest, :string
end
end
class ChangeTypeUserJob < ActiveRecord::Migration[6.0]
def change
rename_column :user_jobs, :job_type, :type
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_01_13_080045) do
ActiveRecord::Schema.define(version: 2020_01_15_032331) do
create_table "cities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "name"
......@@ -71,7 +71,7 @@ ActiveRecord::Schema.define(version: 2020_01_13_080045) do
create_table "user_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.integer "user_id"
t.integer "job_id"
t.integer "job_type"
t.integer "type"
t.datetime "favorited_at"
t.datetime "viewed_at"
t.datetime "applied_at"
......@@ -96,7 +96,6 @@ ActiveRecord::Schema.define(version: 2020_01_13_080045) do
t.string "first_name"
t.string "last_name"
t.text "cv_url"
t.string "password_digest"
t.integer "role"
t.string "remember_digest"
t.datetime "activated_at"
......
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