招聘網(wǎng)站:11-2簡(jiǎn)歷部分

  • 可送出簡(jiǎn)歷:/jobs/1/resumes/new
  • 簡(jiǎn)歷內(nèi)容不為空
  • 使用者得登入才能提交簡(jiǎn)歷
  • 使用attachment欄位

Step 1: 建立投遞簡(jiǎn)歷的鏈接

app/views/jobs/show.html.erb

<div class="text-center">
  <%= link_to("投交履歷", "#",  :style => 
"font-size: 30px; text-decoration: underline;" %>
</div>

Step2: 產(chǎn)生簡(jiǎn)歷Resume 的 model

rails g model resuem job_id:integer user_id:integer content:text
rake db:migrate

step3: 將Resume 與User/Job連起來(lái)

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: 建立簡(jiǎn)歷表單

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 %>

簡(jiǎn)歷內(nèi)容不為空

app/models/resume.rb

class Resume < ApplicationRecord
   belongs_to :user
   belongs_to :job
   validates :content, presence: true
end

Step5: 加入簡(jiǎn)歷上傳功能

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

嘗試提交簡(jiǎn)歷。

Step9

.gitignore加入
public/uploads
最后commit

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Awesome Ruby Toolbox Awesome A collection of awesome Ruby...
    debbbbie閱讀 3,087評(píng)論 0 3
  • compile不常用,對(duì)指令本身的模板做轉(zhuǎn)換,如輸出n次某html片段。他返回一個(gè)link函數(shù),寫(xiě)了compile...
    Jalon閱讀 257評(píng)論 0 0
  • 1.有默認(rèn)值的參數(shù)都不是尾參數(shù)。這時(shí),無(wú)法只省略該參數(shù),而不省略它后面的參數(shù),除非顯式輸入undefined。 函...
    木中木閱讀 256評(píng)論 0 0
  • 所謂遷移學(xué)習(xí),就是將一個(gè)問(wèn)題上訓(xùn)練好的模型,通過(guò)簡(jiǎn)單的調(diào)整,使其適用于一個(gè)新的問(wèn)題的過(guò)程。 一、遷移學(xué)習(xí)的特點(diǎn) 1...
    Midorra閱讀 1,198評(píng)論 0 2
  • 看了一篇文章,關(guān)于過(guò)年,增值。人與人之間的交流總是圍繞著許多俗不可耐卻有及其有生命力的話題,有時(shí)襲面而來(lái),令人們發(fā)...
    石川河女神閱讀 129評(píng)論 0 0

友情鏈接更多精彩內(nèi)容