我有一個Rails應用程序(工作板站點),包含作業模型,控制器和視圖(索引/顯示)。我在我的模型中添加了一個apply_to_email:字符串,該字符串連接到一個按鈕(作業帖子/顯示視圖中的「應用」)。我正在嘗試使用單獨的控制器,視圖和模型爲此創建聯繫表單(稱爲「應用」)。見下文。如何在Jobs Show頁面中顯示以下表單,並且發送的消息中包含有關該職位的信息?我需要發送到apply_to_email的表單。我認爲最終結果應該類似www.example.com/jobs/job-post-title/apply(顯示下面的聯繫表單)。Rails包含表格
非常感謝!
apply.rb
class Apply < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message, :validate => true
attribute :nickname, :captcha => true
def headers
{
:subject => "Contact Form",
:to => "[email protected]",
:from => %("#{name}" <#{email}>)
}
end
end
apply_controller.rb
class ApplyController < ApplicationController
def new
@apply = Apply.new
end
def create
@apply = Apply.new(params[:apply])
@apply.request = request
if @apply.deliver
flash.now[:error] = nil
else
flash.now[:error] = 'Cannot send application.'
render :new
end
end
end
應用/ new.html.erb
<div class="apply-form">
<%= form_for @apply do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name, required: true %>
<br>
<%= f.label :email %><br>
<%= f.email_field :email, required: true %>
<br>
<%= f.label :message %><br>
<%= f.text_area :message, as: :text %>
<div class="hidden">
<%= f.label :nickname %><br>
<%= f.text_field :nickname, hint: 'leave this field blank' %>
</div>
<%= f.submit 'Send', class: "button" %>
<% end %>
</div>
應用/ create.html.erb
<div class="apply-success">
<h1>Thank you for your application!</h1>
<p>The employer will get back to you soon.</p>
</div>
的routes.rb
Rails.application.routes.draw do
resources :jobs
resources :apply, only: [:new, :create]
root 'landing#index'
end