Commit 13c666b9 by Quang Vinh Nguyen

fix almost error from review

parent 70f021b1
# frozen_string_literal: true # frozen_string_literal: true
class EntriesController < ApplicationController class EntriesController < ApplicationController
before_action :set_entry, only: [:show, :edit, :update, :destroy] before_action :set_entry, only: [:show, :edit, :update]
before_action :entry_params, only: [:create]
# GET /entries
# GET /entries.json
def index
@entries = Entry.all
end
# GET /entries/1 # GET /entries/1
# GET /entries/1.json # GET /entries/1.json
...@@ -15,16 +10,16 @@ class EntriesController < ApplicationController ...@@ -15,16 +10,16 @@ class EntriesController < ApplicationController
# GET /entries/new # GET /entries/new
def new def new
if params[:entry_name].blank? && params[:entry_email] @entry = if user_signed_in?
@entry = Entry.new Entry.new(entry_name: current_user.name,
entry_email: current_user.email,
entry_phone: current_user.phone,
entry_address: current_user.address)
else else
@entry = Entry.new(entry_name: params[:entry_name], entry_email: params[:entry_email]) Entry.new
end end
end end
# GET /entries/1/edit
def edit; end
# POST /entries # POST /entries
# POST /entries.json # POST /entries.json
def create def create
...@@ -33,18 +28,19 @@ class EntriesController < ApplicationController ...@@ -33,18 +28,19 @@ class EntriesController < ApplicationController
@job = Job.find(params[:job_id]) @job = Job.find(params[:job_id])
@user = User.find_by(email: params[:entry][:entry_email]) || @user = User.find_by(email: params[:entry][:entry_email]) ||
User.create(name: params[:entry][:entry_name], User.create( name: params[:entry][:entry_name],
email: params[:entry][:entry_email], email: params[:entry][:entry_email],
password: 'password', phone: params[:entry][:entry_phone],
password_confirmation: 'password') address: params[:entry][:entry_address])#,
# password: 'password',
# password_confirmation: 'password')
@entry.user_id = @user.id @entry.user_id = @user.id
@entry.job_id = @job.id @entry.job_id = @job.id
if @user.jobs.include?(@job) if @user.jobs.find_by(id: @job.id)
# 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.' } redirect_to job_url(@job), flash: { secondary: 'You has been entry this job.' }
elsif else
if @entry.save if @entry.save
UserMailer.job_apply(@user, @job).deliver_now UserMailer.job_apply(@user, @job).deliver_now
redirect_to @entry, flash: { secondary: 'Thank you for apply. Please check your email.' } redirect_to @entry, flash: { secondary: 'Thank you for apply. Please check your email.' }
...@@ -64,13 +60,6 @@ class EntriesController < ApplicationController ...@@ -64,13 +60,6 @@ class EntriesController < ApplicationController
end end
end end
# DELETE /entries/1
# DELETE /entries/1.json
def destroy
@entry.destroy
redirect_to entries_url, notice: 'Entry was successfully destroyed.'
end
private private
# Use callbacks to share common setup or constraints between actions. # Use callbacks to share common setup or constraints between actions.
...@@ -80,6 +69,6 @@ class EntriesController < ApplicationController ...@@ -80,6 +69,6 @@ class EntriesController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through. # Never trust parameters from the scary internet, only allow the white list through.
def entry_params def entry_params
params.require(:entry).permit(:entry_name, :entry_email) params.require(:entry).permit(:entry_name, :entry_email, :entry_phone, :entry_address)
end end
end end
...@@ -15,8 +15,6 @@ class Job < ApplicationRecord ...@@ -15,8 +15,6 @@ class Job < ApplicationRecord
dependent: :destroy dependent: :destroy
has_many :users, through: :entries has_many :users, through: :entries
has_many :entries, dependent: :destroy
has_many :favorite_jobs, dependent: :destroy
validates :title, presence: true, length: { maximum: 255 }, validates :title, presence: true, length: { maximum: 255 },
uniqueness: { case_sensitive: false } uniqueness: { case_sensitive: false }
......
# frozen_string_literal: true
class User < ApplicationRecord class User < ApplicationRecord
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable # :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :recoverable, :rememberable, :validatable#,
:confirmable # :confirmable
has_many :entries, dependent: :destroy
has_many :favorite_jobs, dependent: :destroy has_many :favorite_jobs, dependent: :destroy
has_many :entries, foreign_key: 'user_id', has_many :entries, foreign_key: 'user_id',
......
...@@ -35,9 +35,10 @@ ...@@ -35,9 +35,10 @@
<%= f.submit "Update" %> <%= f.submit "Update" %>
</div> </div>
<% end %> <% end %>
<!--
<h3>Cancel my account</h3> <h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p> <p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back %> <%= link_to "Back", :back %>
-->
\ No newline at end of file
json.extract! entry, :id, :entry_name, :entry_email, :created_at, :updated_at
json.url entry_url(entry, format: :json)
...@@ -14,10 +14,16 @@ ...@@ -14,10 +14,16 @@
<%= hidden_field_tag :job_id, @job.id %> <%= hidden_field_tag :job_id, @job.id %>
<%= form.label :name %><br> <%= form.label :name %><br>
<%= form.text_field :entry_name, class: 'form-control col-sm-4' %><br> <%= form.text_field :entry_name, class: 'form-control col-sm-6' %><br>
<%= form.label :email %><br> <%= form.label :email %><br>
<%= form.email_field :entry_email, class: 'form-control col-sm-4' %><br> <%= form.email_field :entry_email, class: 'form-control col-sm-6' %><br>
<%= form.label :phone %><br>
<%= form.telephone_field :entry_phone, class: 'form-control col-sm-6' %><br>
<%= form.label :address %><br>
<%= form.text_area :entry_address, class: 'form-control col-sm-6' %><br>
<%= form.submit yield(:button_text), class: "btn btn-secondary" %> <%= form.submit yield(:button_text), class: "btn btn-secondary" %>
<%= link_to 'Back', :back, class: "btn btn-secondary" %> <%= link_to 'Back', :back, class: "btn btn-secondary" %>
......
<p id="notice"><%= notice %></p>
<h1>Entries</h1>
<table>
<thead>
<tr>
<th>Entry name</th>
<th>Entry email</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @entries.each do |entry| %>
<tr>
<td><%= entry.entry_name %></td>
<td><%= entry.entry_email %></td>
<td><%= link_to 'Show', entry %></td>
<td><%= link_to 'Edit', edit_entry_path(entry) %></td>
<td><%= link_to 'Destroy', entry, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Entry', new_entry_path %>
json.array! @entries, partial: 'entries/entry', as: :entry
...@@ -11,16 +11,10 @@ ...@@ -11,16 +11,10 @@
<%= @job.description %> <%= @job.description %>
</p> </p>
<% 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), <%= link_to "Apply", apply_path(job_id: @job.id),
class: "btn btn-secondary" %> class: "btn btn-secondary" %>
<% end %>
<%= link_to 'Back', :back, class: "btn btn-secondary" %> <%= link_to 'Back', :back, class: "btn btn-secondary" %>
</section> </section>
</aside> </aside>
</div> </div>
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<p>Job title: <%= @job.title %></p> <p>Job title: <%= @job.title %></p>
<p>Location: <%= @job.cities.first.name %></p> <p>Location: <%= @job.cities.first.name %></p>
<p>Company: <%= Company.find(@job.company_id).name %></p> <p>Company: <%= @job.company.name %></p>
<p>Your submitted information:</p> <p>Your submitted information:</p>
<p>Full Name: <%= @user.name %></p> <p>Full Name: <%= @user.name %></p>
......
...@@ -2,7 +2,7 @@ Dear <%= @user.name %> ...@@ -2,7 +2,7 @@ Dear <%= @user.name %>
Job title: <%= @job.title %> Job title: <%= @job.title %>
Location: <%= @job.cities.first.name %> Location: <%= @job.cities.first.name %>
Company: <%= Company.find(@job.company_id).name %> Company: <%= @job.company.name %>
Your submitted information: Your submitted information:
Full Name: <%= @user.name %> Full Name: <%= @user.name %>
......
Rails.application.routes.draw do Rails.application.routes.draw do
root to: 'jobs#home' root to: 'jobs#home'
resources :entries resources :entries, only: [:new, :create, :show, :edit, :update]
get '/apply', to: 'entries#new' get '/apply', to: 'entries#new'
resources :jobs do resources :jobs do
......
...@@ -8,6 +8,7 @@ class DeviseCreateUsers < ActiveRecord::Migration[5.2] ...@@ -8,6 +8,7 @@ class DeviseCreateUsers < ActiveRecord::Migration[5.2]
t.string :prefix, default: '' t.string :prefix, default: ''
t.string :phone, default: '' t.string :phone, default: ''
t.boolean :registration, default: false t.boolean :registration, default: false
t.text :address
## Database authenticatable ## Database authenticatable
t.string :email, null: false, default: '' t.string :email, null: false, default: ''
......
...@@ -5,6 +5,8 @@ class CreateEntries < ActiveRecord::Migration[5.2] ...@@ -5,6 +5,8 @@ class CreateEntries < ActiveRecord::Migration[5.2]
t.references :job, foreign_key: true t.references :job, foreign_key: true
t.string :entry_name t.string :entry_name
t.string :entry_email t.string :entry_email
t.string :entry_phone
t.text :entry_address
t.timestamps t.timestamps
end end
......
...@@ -52,6 +52,8 @@ ActiveRecord::Schema.define(version: 2018_06_19_011959) do ...@@ -52,6 +52,8 @@ ActiveRecord::Schema.define(version: 2018_06_19_011959) do
t.bigint "job_id" t.bigint "job_id"
t.string "entry_name" t.string "entry_name"
t.string "entry_email" t.string "entry_email"
t.string "entry_phone"
t.text "entry_address"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["job_id"], name: "index_entries_on_job_id" t.index ["job_id"], name: "index_entries_on_job_id"
...@@ -111,6 +113,7 @@ ActiveRecord::Schema.define(version: 2018_06_19_011959) do ...@@ -111,6 +113,7 @@ ActiveRecord::Schema.define(version: 2018_06_19_011959) do
t.string "prefix", default: "" t.string "prefix", default: ""
t.string "phone", default: "" t.string "phone", default: ""
t.boolean "registration", default: false t.boolean "registration", default: false
t.text "address"
t.string "email", default: "", null: false t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false t.string "encrypted_password", default: "", null: false
t.string "reset_password_token" t.string "reset_password_token"
......
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