Chapter 4-5. Creating custom website for hire workers
Bravo webdevHi there. This is 4th chapter of our tutorial "Creating custom website for hire workers".
previous chapters available at out telegram channel ang group.
https://t.me/bravowebdevchannel
https://t.me/bravowebdev
Today we will complete our registration and we will add for headhunter new feature - post a job. And for worker - apply to a job. To do this, we will create new resource - application.
add to jobs controller
class JobsController < ApplicationController
before_action :require_headhunter_login!, only: [:show,:new,:destroy]
def index
end
def show
@job = Job.find(params[:id])
end
def new
@headhunter = Headhunter.find(current_headhunter.id)
@job = @headhunter.jobs.new
@job.status = 0
end
def create
@headhunter = Headhunter.find(current_headhunter.id)
@job = @headhunter.jobs.new(job_params)
if @job.save
redirect_to @job
else
render :new
end
end
def destroy
@job = Job.find(params[:id])
@job.destroy
redirect_to root_path
end
def edit
@job = Job.find(params[:id])
end
def update
@job = Job.find(params[:id])
if @job.update(job_params)
redirect_to @job
else
render :edit
end
end
private
def job_params
params.require(:job).permit(:title, :description)
end
end
Now we need add application (applies) resource to make an apply for workers. And show list of applicants to headhunters page.
Application have applicant (worker) id and job id. Worker also will see his applies at his profile.
add to job model
has_many :applies, as: :applicable
add to worker model
has_many :applies, as: :applicable
add to apply model
belongs_to :applicable, :polymorphic => true
type
rails g resource Apply
add to migration
rails g migration add_to_applies
add_column :applies, :applicable_id, :bigint
add_column :applies, :applicable_type, :string
rails db:migrate
add to applies controller
class AppliesController < ApplicationController
before_action :require_worker_login!, only: [:index, :new, :create]
def index
@worker = Worker.find(current_worker.id)
@applies = @worker.applies
end
def new
@worker = Worker.find(current_worker.id)
@apply = @worker.applies.new
end
def create
@worker = Worker.find(current_worker.id)
@apply = @worker.applies.new
@job = Job.find(params[:job_id])
@apply.applicable = @job
if @apply.save
flash[:notice] = "Successfull save apply"
redirect_to workers_path
else
flash[:notice] = "Error saving apply"
rendirect_to workers_path
end
end
end
add _form view to jobs
<%= form_with model: @job |form| do%>
<div>
<%= form.label :title %> <br/>
<%= form.text_field :title %> <br/>
<% jobs.errors.full_messages_for(:title).each do |message|%>
<div><%= message %></div> <% end %>
</div>
<div>
<%= form.label :description %> <br/>
<%= form.text_field :description %> <br/>
<% jobs.errors.full_messages_for(:description).each do |message|%>
<div><%= message %></div> <% end %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
add new.html.erb job
<h1> Create new job </h1>
<%= form_with model: @job do |f| %>
<p>
<%= f.label 'title' %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label 'description' %><br>
<%= f.text_field :description %>
</p>
<p>
<%= f.submit 'Create job' %>
</p>
<% end %>
add edit.html.erb to job
<%= render "layouts/registration" %>
<h1> Edit job </h1>
<%= render "form", jobs: @job %>
add show.html.erb to job
<%= render "layouts/registration" %>
<h1> Job </h1>
<div>
Title: <%= @job.title %> <br/>
Description <%= @job.description %>
</div>
<div>
<%= link_to "Edit", edit_job_path %> <br/>
<%= link_to "Destroy", job_path(@job),
method: :delete,
data: {confirm: "Are you ready?"} %> <br/>
<%= link_to "All jobs", jobs_path %>
</div>
finally add to index.html.erb of jobs this line
<p>
<%= link_to "Create new job", new_job_path %>
</p>
add to index.html.erb workers
<p>
<h1>This is worker profile</h1>
</p>
<p>
<%= link_to "My worker profile", worker_path(current_worker.id) %>
</p>
<div>
<% @jobs.each do |job| %>
<div>
Title: <%= job.title %> <br/>
<%= link_to "See more", job_path(job.id) %> <br/>
<%= link_to "Apply", applies_path(job_id: job.id), :method => :post %>
</div>
<br/>
<% end %>
</div>
now our worker controllers looks like this
class WorkersController < ApplicationController
before_action :require_worker_login!, only: [:destroy, :show, :index, :edit]
def index
@jobs = Job.all
end
def new
@Worker = Worker.new
end
def create
@Worker = Worker.new(worker_params)
if @Worker.save
redirect_to @Worker
else
render :new
end
end
def show
@Worker = Worker.find(params[:id])
end
def edit
@Worker = Worker.find(params[:id])
end
def update
@Worker = Worker.find(params[:id])
if @Worker.update(worker_params)
redirect_to @Worker
else
render :edit
end
end
def applies
@worker = Worker.find(params[:id])
@applies = Apply.where("applicable_type = ? and applicable_id = ?",
"Worker",@worker.id)
applies_arr = []
apply_jobs_arr = []
@applies.each {|apply| applies_arr.push apply.id; apply_jobs_arr.push (apply.id - 1) }
apply_jobs = Apply.find(apply_jobs_arr)
jobs_arr = []
apply_jobs.each {|apply| jobs_arr.push apply.applicable_id }
@jobs = Job.find(jobs_arr)
end
private
def worker_params
params.require(:worker).permit(:name, :contact)
end
end
and we can register at our website as worker or as headhunter, post a job and make a reply-apply. We need rewrite templates (views) using Bootstrap or other thing. Also we need rewrite apply system not with polymorhic relation but with many_to thrown with saving job_id and worker_id at apply. Alsow we need to add admin user.
That it. Thank you for reading.
Github
https://github.com/RaptorialThing/bravo-webdev
Deployed
https://bravo-webdev.herokuapp.com/
Join us
t.me/bravowebdevchannel
t.me/bravowebdev