【发布时间】:2017-02-16 11:04:28
【问题描述】:
我有一个带有工作模型、控制器和视图(索引/显示)的 rails 应用程序(一个工作板网站)。我在我的模型中包含了一个 apply_to_email:string,它连接到一个按钮(职位发布/显示视图中的“应用”)。我正在尝试使用单独的控制器、视图和模型为此创建一个联系表单(称为“应用”)。见下文。如何在“职位显示”页面中提供以下表格,并在发送的消息中包含有关职位发布的信息?我需要将表格发送到 apply_to_email。我认为最终结果应该类似于 www.example.com/jobs/job-post-title/apply (显示下面的联系表)。
提前非常感谢!
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 => "example@example.com",
: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>
申请/创建.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
【问题讨论】:
标签: ruby-on-rails ruby forms