Commit fc55b92c by Quang Vinh Nguyen

update ActiveRecord -- RecordNotFound handle

parent 08d9d237
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
protected
def record_not_found
respond_to do |format|
format.html { render file: "#{Rails.root}/public/404",
layout: false,
status: :not_found }
format.xml { head :not_found }
format.any { head :not_found }
end
end
end
# frozen_string_literal: true
class JobsController < ApplicationController
# rescue_from ActiveRecord::RecordNotFound, with: :job_not_found
def index
search_str = params[:search].blank? ? '*' : params[:search]
job_found = Job.search(kw: search_str, page: params[:page].to_i)
@job_ids = job_found[:job_id]
@job_num = job_found[:num]
job_search_result = Job.where(id: @job_ids)
search_str = params[:search].blank? ? '*' : params[:search]
job_found = Job.search(kw: search_str, page: params[:page].to_i)
job_ids = job_found[:job_id]
@job_num = job_found[:num]
job_search_result = Job.where(id: job_ids)
@jobs = Kaminari.paginate_array(job_search_result,
total_count: @job_num).
page(params[:page]).
per(Kaminari.config.default_per_page)
page(params[:page])
end
def show
@job = Job.find_by(id: params[:id])
unless @job.nil?
return @job
else
job_not_found(params[:id])
end
@job = Job.find(params[:id])
end
def city
......@@ -37,9 +30,4 @@ class JobsController < ApplicationController
@jobs = Job.all.order(updated_at: :desc).take(5)
@cities = City.all.select { |city| city.jobs.any? }.take(8)
end
def job_not_found(id)
redirect_to root_path
flash[:danger] = "Couldn't find job id: #{id}"
end
end
# frozen_string_literal: true
class Job < ApplicationRecord
attr_accessor :page_per
belongs_to :company, foreign_key: 'company_id'
has_many :cities_jobs, foreign_key: 'job_id',
......@@ -34,8 +33,6 @@ class Job < ApplicationRecord
validates :condition, presence: true, length: { maximum: 1000 }
validates :link, presence: true
@page_per = 10
paginates_per @page_per
def self.search(search_hash = { kw: '*', page: 0})
jobs = { job_id: [], job_num: 0 }
......
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