Commit 70f021b1 by Quang Vinh Nguyen

job apply feature

parent f5664c95
# frozen_string_literal: true
class EntriesController < ApplicationController
before_action :set_entry, only: [:show, :edit, :update, :destroy]
......@@ -9,40 +11,43 @@ class EntriesController < ApplicationController
# GET /entries/1
# GET /entries/1.json
def show
end
def show; end
# GET /entries/new
def new
@entry = Entry.new
if params[:entry_name].blank? && params[:entry_email]
@entry = Entry.new
else
@entry = Entry.new(entry_name: params[:entry_name], entry_email: params[:entry_email])
end
end
# GET /entries/1/edit
def edit
end
def edit; end
# POST /entries
# POST /entries.json
def create
@entry = Entry.new(entry_params)
@job = Job.find(params[:job_id])
@user = User.find_by( email: params[:entry][:entry_email]) ||
User.create( name: params[:entry][:entry_name],
email: params[:entry][:entry_email],
password: 'password',
password_confirmation: 'password')
@user = User.find_by(email: params[:entry][:entry_email]) ||
User.create(name: params[:entry][:entry_name],
email: params[:entry][:entry_email],
password: 'password',
password_confirmation: 'password')
@entry.user_id = @user.id
@entry.job_id = @job.id
if @user.jobs.include?(@job)
redirect_to job_url(@job), notice: 'You has been entry this job.'
# redirect_to job_url(@job), notice: 'You has been entry this job.'
redirect_to job_url(@job), flash: { secondary: 'You has been entry this job.' }
elsif
if @entry.save
UserMailer.job_apply(@user, @job).deliver_now
redirect_to @entry, notice: 'Email sent to you, Thank you for apply.'
redirect_to @entry, flash: { secondary: 'Thank you for apply. Please check your email.' }
else
render :new
end
......@@ -52,11 +57,11 @@ class EntriesController < ApplicationController
# PATCH/PUT /entries/1
# PATCH/PUT /entries/1.json
def update
if @entry.update(entry_params)
redirect_to @entry, notice: 'Entry was successfully updated.'
else
render :edit
end
if @entry.update(entry_params)
redirect_to @entry, notice: 'Entry was successfully updated.'
else
render :edit
end
end
# DELETE /entries/1
......@@ -67,13 +72,14 @@ class EntriesController < ApplicationController
end
private
# Use callbacks to share common setup or constraints between actions.
def set_entry
@entry = Entry.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def entry_params
params.require(:entry).permit(:entry_name, :entry_email)
end
# Use callbacks to share common setup or constraints between actions.
def set_entry
@entry = Entry.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def entry_params
params.require(:entry).permit(:entry_name, :entry_email)
end
end
<%= form_with(model: entry, local: true) do |form| %>
<% if entry.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(entry.errors.count, "error") %> prohibited this entry from being saved:</h2>
<h5><%= pluralize(entry.errors.count, "error") %> prohibited this entry from being saved:</h5>
<ul>
<% entry.errors.full_messages.each do |message| %>
......@@ -14,11 +14,11 @@
<%= hidden_field_tag :job_id, @job.id %>
<%= form.label :name %><br>
<%= form.text_field :entry_name, class: 'form-control' %><br>
<%= form.text_field :entry_name, class: 'form-control col-sm-4' %><br>
<%= form.label :email %><br>
<%= form.email_field :entry_email, class: 'form-control' %><br>
<%= form.submit yield(:button_text), class: "btn btn-primary" %>
<%= form.email_field :entry_email, class: 'form-control col-sm-4' %><br>
<%= form.submit yield(:button_text), class: "btn btn-secondary" %>
<%= link_to 'Back', :back, class: "btn btn-secondary" %>
<% end %>
......@@ -2,7 +2,4 @@
<% @job = Job.find(@entry.job_id) %>
<h1>Editing Entry</h1>
<%= render 'form', entry: @entry %>
<%= link_to 'Show', @entry %> |
<%= link_to 'Back', entries_path %>
<%= render 'form', entry: @entry %>
\ No newline at end of file
......@@ -11,5 +11,5 @@
<%= @entry.entry_email %>
</p>
<%= link_to 'Edit', edit_entry_path(id: @entry.id) %> |
<%= link_to 'Back', entries_path %>
<%= link_to 'Edit', edit_entry_path(id: @entry.id), class: "btn btn-secondary" %>
<%= link_to 'Back', job_path(@job), class: "btn btn-secondary" %>
......@@ -11,9 +11,16 @@
<%= @job.description %>
</p>
<%= link_to "Apply", apply_path(job_id: @job.id),
class: "btn btn-primary" %>
<% if user_signed_in? %>
<%= link_to "Apply", { controller: "entries", action: "new", job_id: @job.id,
entry_name: current_user.name, entry_email: current_user.email },
class: "btn btn-secondary" %>
<% else %>
<%= link_to "Apply", apply_path(job_id: @job.id),
class: "btn btn-secondary" %>
<% end %>
<%= link_to 'Back', :back, class: "btn btn-secondary" %>
</section>
</aside>
</div>
Rails.application.routes.draw do
root to: 'jobs#home'
resources :entries
get '/apply', to: 'entries#new'
resources :jobs do
collection do
......
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