Padrino:如何将表单数据发送到app方法

时间:2014-06-27 09:11:26

标签: ruby padrino

我在将表单中的变量发送到发送电子邮件的post方法时遇到了一些麻烦。该方法接收变量名而不是其内容。

表格

= form_tag url(:amenities, :email_booking, name: :name, email_addr: :email, message: :message), :method => 'post' do

  = flash_tag(:notice)
  = field_set_tag do
    %p
      = text_field_tag :name, :placeholder => "Please enter your name"
    %p
      = text_field_tag :email, :placeholder => "Please enter your email address",
    %p
      = text_area_tag :message, :placeholder => "Message"

  = field_set_tag(:class => 'buttons') do
    = submit_tag 'SEND MESSAGE'

控制器

TourApart::App.controllers :amenities do

  post :email_booking, with: [:name, :email_addr, :message] do

    name = params[:name]
    email_addr = params[:email_addr]
    message = params[:message]

    email do
      from "bookings@example.com"
      cc "customer@example.com"
      to email_addr
      subject "We hope to see you soon"
      locals :name => name, :message => message

      body render('tour_booking_email')
      content_type :plain
    end
    render "/"
  end


end

这将(使用模板,未显示)生成并发送看起来像

的电子邮件
  

DEBUG - 发送电子邮件至:email bernardo.santos.83@gmail.com
  日期:2014年6月27日星期五09:48:12 +0100来自:bookings@example.com
  收件人:发送电子邮件至cc:customer@example.com消息ID:   < 53ad2fcc7d2b7_eaaa3fe59362362c7528e@MK-XI.local.mail>主题:我们   希望很快见到你Mime-Version:1.0 Content-Type:text / plain;
  charset = UTF-8 Content-Transfer-Encoding:7bit

 Dear name,
 We will contact you very soon. Thank you for your interest in our services. We recieved the following message from you:
 "message"
 Please contact us at bookings@example.com if there is anything you would like to add or clarify."   Sincerely,
puts params[:name] 

在控制器中也会返回“name”,所以我猜测post方法没有收到数据。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

通常你不会使用:with =>对于这种职位形式。

只需使用

# form.haml
form_tag url(:amenities, :email_booking), :method => 'post' do
  ...

TourApart::App.controllers :amenities do
  post :email_booking do
    name = params[:name]
    email_addr = params[:email_addr]
    message = params[:message]
    ...

(或者完全跳过像name => params[:name]这样的副词,取决于你想用它做什么(同样,验证)。 也许这已经揭示了你的问题。