- 可送出簡歷:/jobs/1/resumes/new
- 簡歷內(nèi)容不為空
- 使用者得登入才能提交簡歷
- 使用attachment欄位
Step 1: 建立投遞簡歷的鏈接
app/views/jobs/show.html.erb
<div class="text-center">
<%= link_to("投交履歷", "#", :style =>
"font-size: 30px; text-decoration: underline;" %>
</div>
Step2: 產(chǎn)生簡歷Resume 的 model
rails g model resuem job_id:integer user_id:integer content:text
rake db:migrate
step3: 將Resume 與User/Job連起來
app/models/resume.rb
class Resume < ApplicationRecord
...
+belongs_to :user
+belongs_to :job
...
end
app/models/job.rb
class Job < ApplicationRecord
...
+has_many :resume
...
end
app/models/user.rb
class User < ApplicationRecord
...
+has_many :resumes
...
end
Step4: 建立簡歷表單
rails g controller resumes
修改config/routes.rb
Rails.application.routes.draw do
devise_for :users
namespace :admin do
resources :jobs do
member do
post :publish
post :hide
end
end
end
resources :jobs do
resources :resumes
end
root 'job#index'
end
修改show
app/views/jobs/show.html.erb
<div class="text-center">
<%= link_to("投交履歷", new_job_resume_path(@job),
:style => "font-size: 30px;
text-decoration:underline;") %>
</div>
新增resumes_controller
的內(nèi)容
app/controllers/resumes_controller.rb
class ResrmesController < ApplicationController
before_action :authenticate_user!
def new
@job = Job.find(params[:job_id])
@resume = Resume.new
end
def create
@job = Job.find(params[:job_id])
@resume = Resume.new(resume_params)
@resume.job = @job
@resume.user = current_user
if @resume.save
flash[:notice] = "成功提交履歷"
redirect_to job_path(@job)
else
render :new
end
end
private
def resume_params
params.require(:resume).permit(:content)
新增app/views/resumes/new.html.erb
<h3> 投交履歷到 <%= @job.title %> </h3>
<hr>
<%= simple_form_for [@job, @resume] do |f| %>
<%= f.input :content %>
<%= f.submit "送出" %>
<% end %>
簡歷內(nèi)容不為空
app/models/resume.rb
class Resume < ApplicationRecord
belongs_to :user
belongs_to :job
validates :content, presence: true
end
Step5: 加入簡歷上傳功能
Gemfile
gem 'carrierwave'
bundle install
Step6:新增attachment欄位
rails g migration add_attachment_to_resume
內(nèi)容:
class AddAttachmentToResume < ActiveRecord::Migration[5.0]
def change
add_column :resumes, :attachment, :string
end
end
rake db:migrate
Step7 掛上Attachment Uploader 到 Resume 上
rails g uploader attachment
app/models/resume.rb
class Resume < ApplicationRecord
belongs_to :user
belongs_to :job
mount_uploader :attachment, AttachmentUploader
validates :content, presence: true
end
修改:
app/views/resumes/new.html.erb
<%= simple_form_for [@job, @resume] do |f| %>
<%= f.input :content %>
<%= f.input :attachment %>
<%= f.submit "送出" %>
<% end %>
Step8:
app/controllers/resumes_controller.rb
def resume_params
params.require(:resume).permit(:content, :attachment)
end
嘗試提交簡歷较解。
Step9
在.gitignore
加入
public/uploads
最后commit