Chapter4. Creating custom website for hire workers

Chapter4. Creating custom website for hire workers

Bravo webdev

Hi 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 redirect headhunter and workers to their profiles. Also 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.


1) Templates and controllers for headhunter and worker.

We need template to fill headhunters, workers profile data and edit it (every user will able edit only his own profile). And we need CRUD for job.

We already have hr (headhunter),worker portfilio so we need just add edit, destroy buttons to it and, of course, create. And after registration we have to redirect to new hr or worker, after login go back as now.

Lets add new route controller and new view

def new for show create new page for fill data about headhunter and def create to save filled data about headhunter to database, create new headhunter instance, row. And we need new method for check filled data - headhunter_params.

app/controllers/headhunters_controller

 before_action :require_headhunter_login!, :only [:show, :index]

 def new

  @Headhunter = Headhunter.new

 end 

 def create

  @Headhunter = Headhunter.new(headhunter_params)


  if @Headhunter.save

   redirect_to @Headhunter

  else

   render :new

  end 

 end 

 def show

  @Headhunter = Headhunter.find(params[:id])

 end 

 private

 def headhunter_params

  params.require(:headhunter).permit(:name, :contact)

 end 


views/headhunter/new.html.erb


<h1> Create new headhunter</h1>

<p>

 <%= form_with model: @Headhunter do |form | %>

 <p>

  <%= form.label :name %> <br/>

  <%= form.text_field :name %> <br/>

 </p>  

 <p>

  <%= form.label :contact %> <br/>

  <%= form.text_field :contact %> <br/>

 </p>

 <p>

  <%= form.submit %>

 </p>  

 <% end %>

</p>


now we need change registration main page link to headhunter and worker new page and show page (profile page) and protect it from unatuhoraized guests



add to app/controllers/headhunters_controller.rb and workers_controllers.rb


and add app/controllers/application_controller

 before_action :set_current_user

 def set_current_user

  Current.headhunter = Headhunter.find_by(id: session[:headhunter_id]) if session[:headhunter_id]

  Current.worker = Worker.find_by(id: session[:worker_id]) if session[:worker_id]

 end 

 def require_headhunter_login!

  redirect_to new_headhunter_session_path, alert: "You must be logged in" if Current.headhunter.nil?

 end 

 def require_worker_login!

  redirect_to new_worker_session_path, alert: "You must be logged in" if Current.worker.user.nil?

 end 


 and add to views/headhunter/session/new and workers


<%= link_to "Registration", new_headhunter_registration_path %>


same with workers controller and workers view



add views/headhunter/index.html.erb,same workers


 <%= link_to "My headhunter profile", headhunter_path(current_headhunter.id) %>


type

rails g devise:controllers workers -c=sessions 

same to headhunters


add to controllers/headhunter/session_controller


 def create

   headhunter = Headhunter.find_by(email: params[:email])

   # finds existing user, checks to see if user can be authenticated

   if headhunters.present? && headhunter.authenticate(params[:password])

   # sets up user.id sessions

    session[:headhunter_id] = headhunter.id

    redirect_to root_path, notice: 'Logged in successfully'

   else

    flash.now[:alert] = 'Invalid email or password'

    render :new

   end

  end

  def edit

  @Headhunter = Headhunter.find(params[:id])

 end 

 def update

  @Headhunter = Headhunter.find(params[:id])


  if @Headhunter.update(headhunter_params)

   redirect_to @Headhunter

  else

   render :edit 

  end   

 end 

  def destroy

   # deletes user session

   session[:headhunter_id] = nil

   redirect_to root_path, notice: 'Logged Out'

  end

and add same thing to workers session controller


Now we create new model current.rb


 class Current < ActiveSupport::CurrentAttributes

  # makes Current.user accessible in view files.

  attribute :headhunter

  attribute :worker

 end


Change views/headhunter/show.html.erb and workers as

<p>

<h1>This is headhunter profile</h1>

</p>

<p>

Name: <%= @Headhunter.name %>

<br/>

Contact <%= @Headhunter.contact %>

<%= link_to "Edit", edit_headhunter_path %>

</p>


add edit.html.erb and workers




then add _registration.html.erb and registration.html.erb to views/layouts


 <% content_for :registration do -%>

 <% if current_headhunter %>

  Logged in as: <%= current_headhunter.email %><br/>

  <%= link_to 'Edit Password', edit_headhunter_password_path %>

  <%= button_to 'Logout', destroy_headhunter_session_path, method: :delete %>

  <% else %>

  <%= link_to 'Login as headhunter', new_headhunter_session_path %>

 <% end %>

  <% if current_worker %>

  Logged in as: <%= current_worker.email %><br/>

  <%= link_to 'Edit Password', edit_worker_password_path %>

  <%= button_to 'Logout', destroy_worker_session_path, method: :delete %>

  <% else %>

  <%= link_to 'Login as worker', new_worker_session_path %>

 <% end %>

 <% end -%>



add to views/jobs/index.html.erb


<%= render "layouts/registration" %>

<p>This is an article from index.html.erb </p>

<p> <%= link_to "Registration as headhunter", new_headhunter_path %> </p>

<p><%= link_to "Login as headhunter", new_headhunter_session_path %></p>

<p>

 <%= link_to "My headhunter profile", headhunters_path %>

</p>  

<br/>

<p><%= link_to "Registration as worker", new_worker_registration_path %></p>

<p><%= link_to "Login as worker", new_worker_session_path %> </p>

<p>

 <%= link_to "My worker profile", workers_path%>

</p>


Join us


t.me/bravowebdevchannel

t.me/bravowebdev


Report Page