0

I'm having some trouble sending variables from a form to a post method which sends emails. The method receives the variable names instead of their content.

Form

= 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'

Controller

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

this will (with the template, not shown) generate and send an email that looks like

DEBUG - Sending email to: email bernardo.santos.83@gmail.com
Date: Fri, 27 Jun 2014 09:48:12 +0100 From: bookings@example.com
To: email Cc: customer@example.com Message-ID: <53ad2fcc7d2b7_eaaa3fe59362362c7528e@MK-XI.local.mail> Subject: We hope to see you soon 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] 

in the controller will also return "name", so i'm guessing the post method is not receiving the data.

Any ideas?

Kimmo Lehto
  • 5,910
  • 1
  • 23
  • 32
bcsantos
  • 2,635
  • 5
  • 21
  • 22

1 Answers1

0

Usually you would not use the :with => for this kind of post form.

Just use

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

and

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

(Or skip the passignments like name => params[:name]alltogether, depends a bit on what you want to do with it (also, validation). Maybe that sheds light on your problem already.

Felix
  • 4,510
  • 2
  • 31
  • 46