- 可送出简历:/jobs/1/resumes/new
- 简历内容不为空
- 使用者得登入才能提交简历
- 使用attachment栏位
Step 1: 建立投递简历的链接
app/views/jobs/show.html.erb
<div class="text-center">
<%= link_to("投交履历", "#", :style =>
"font-size: 30px; text-decoration: underline;" %>
</div>
Step2: 产生简历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
的内容
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 %>
简历内容不为空
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
内容:
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