Commit 49e65316 by Mai Hoang Thai Ha

add slug to industries and cities

parent 0442a3fc
......@@ -44,8 +44,6 @@ group :development do
gem 'listen', '~> 3.3'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'nokogiri', '~> 1.11', '>= 1.11.7'
gem 'httparty', '~> 0.18.1'
gem 'rubocop-rails', '~> 2.11', '>= 2.11.3'
end
......@@ -60,4 +58,6 @@ end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'slim-rails', '~> 3.2'
gem 'kaminari', :git => 'https://github.com/kaminari/kaminari'
\ No newline at end of file
gem 'kaminari', :git => 'https://github.com/kaminari/kaminari'
gem 'nokogiri', '~> 1.11', '>= 1.11.7'
gem 'httparty', '~> 0.18.1'
\ No newline at end of file
class JobsController < ApplicationController
def index
if params[:city_id].present?
city = City.find(params[:city_id])
if params[:city_slug].present?
city = City.find_by(slug: params[:city_slug])
@name = city.name
@jobs = city.jobs
elsif params[:industry_id].present?
industry = Industry.find(params[:industry_id])
elsif params[:industry_slug].present?
industry = Industry.find_by(slug: params[:industry_slug])
@name = industry.name
@jobs = industry.jobs
else
......
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def to_slug(string)
string.parameterize.truncate 80, omission: ''
end
end
class City < ApplicationRecord
has_and_belongs_to_many :jobs
enum region: %i[Vietnam International]
validates :slug, presence: true, uniqueness: { case_sensitive: true }
before_validation :add_slugs
def to_prarams
slug
end
private
def add_slugs
update slug: to_slug(name)
end
end
class Industry < ApplicationRecord
has_and_belongs_to_many :jobs
validates :slug, presence: true, uniqueness: { case_sensitive: true }
before_validation :add_slugs
def to_prarams
slug
end
private
def add_slugs
update slug: to_slug(name)
end
end
......@@ -14,5 +14,5 @@
.col-lg-4.col-md-6.text-center
.mt-5
.city-item
h3.h4.mb-2.see-more-text= link_to city, city_jobs_path(City.find_by(name: city).id) , class:'text-decoration-none fw-normal text-reset'
h3.h4.mb-2.see-more-text= link_to city, city_jobs_path(City.find_by(name: city).slug) , class:'text-decoration-none fw-normal text-reset'
p.text-muted.mb-0= pluralize(count, "job")
\ No newline at end of file
......@@ -6,7 +6,7 @@
- @industries_job_list.each do |name, job_count|
.col-lg-6.col-md-6.text-center
.mt-5
h3.h4.mb-2.see-more-text= link_to name, industry_jobs_path(Industry.find_by(name: name).id) , class:'text-decoration-none fw-normal text-reset'
h3.h4.mb-2.see-more-text= link_to name, industry_jobs_path(Industry.find_by(name: name).slug) , class:'text-decoration-none fw-normal text-reset'
p.text-muted.mb-0= pluralize(job_count, "job")
hr.divider.my-4
......@@ -4,7 +4,7 @@
.row.align-items-start.mb-3
- @top_cities.each do |city, city_jobs|
.col-4.city-item
= link_to city.name, city_jobs_path(City.find_by(name: city.name).id), class: 'city-name'
= link_to city.name, city_jobs_path(City.find_by(name: city.name).slug), class: 'city-name'
span.job-count
| (
= pluralize(city_jobs, "job")
......
......@@ -4,7 +4,7 @@
.row.align-items-start
- @top_industries.each do |industry, industry_jobs|
.col-4.industry-item
= link_to industry.name, industry_jobs_path(Industry.find_by(name: industry.name).id), class: 'industry-name'
= link_to industry.name, industry_jobs_path(Industry.find_by(name: industry.name).slug), class: 'industry-name'
span.job-count
| (
= pluralize(industry_jobs, "job")
......
Rails.application.routes.draw do
root 'top#index'
resources :cities, only: %i[index show] do
resources :cities, param: :slug, only: %i[index show] do
resources :jobs, only: %i[index]
end
resources :industries, only: %i[index show] do
resources :industries, param: :slug, only: %i[index show] do
resources :jobs, only: %i[index]
end
......
class AddSlugToIndustries < ActiveRecord::Migration[6.1]
def change
add_column :industries, :slug, :string
add_index :industries, :slug, unique: true
end
end
class AddSlugToCities < ActiveRecord::Migration[6.1]
def change
add_column :cities, :slug, :string
add_index :cities, :slug, 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: 2021_07_20_055614) do
ActiveRecord::Schema.define(version: 2021_07_22_080023) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false
......@@ -54,6 +54,7 @@ ActiveRecord::Schema.define(version: 2021_07_20_055614) do
t.integer "region"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "slug"
end
create_table "cities_jobs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
......@@ -93,6 +94,7 @@ ActiveRecord::Schema.define(version: 2021_07_20_055614) do
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "slug"
end
create_table "industries_jobs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
......
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