2017-02-16 77 views
0

我有一个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 

回答

0

您可以使用job/show.html.erb#render方法来呈现从另一个控制器的模板。 (在你的情况ApplyController)

请参阅从the rails docs

2.2.2 Rendering an Action's Template from Another Controller在你的情况下,它看起来像:

<%= render 'apply/new' %>