rmagnum2002
rmagnum2002

Reputation: 11421

rails 3 mailer getting data from form

I have an e-mail action.. simple link:

<%= link_to 'Send offer by mail',  offer_to_mail_car_path(@car) %>

this one should send a notification to the admin mail that some client is offering a specific amount of money for this car. So the client must insert in to a form his email and his offer. This data are not stored in database, it's just for sending email and clear out. So for now I do recieve email with car data, name, urls to pictures etc.. but how do I build a form to show up those 2 fields with clients email and offer, how the controller will look and the link itself. Thanks for your priceless time.

controller:

def offer_to_mail
    @car = Car.find(params[:id])
    CarMailer.offer_to_mail(@car).deliver
    redirect_to @car, :notice  => "Offer sent."
  end

Upvotes: 1

Views: 1552

Answers (1)

rmagnum2002
rmagnum2002

Reputation: 11421

I got the answer thanks to a friend of mine. I'll post here the solution cause may be some one need this too.

in car mailers will do

def request_by_mail(car, your_name, your_message)
    @car = car
    @name = your_name
    @message = your_message
    @url  = "http://cardealer.com/cars"
  #  attachments["rails.png"] = File.read("#{Rails.root}/public/images/rails.png")
    mail(:to => '[email protected]',
         :subject => "Car details request from a client",
         :date => Time.now
         )
  end

in cars_controller

def request_by_mail
    @car = Car.find(params[:id])
    mail = params[:request_by_mail][:your_mail]
    message = params[:request_by_mail][:your_message]
    CarMailer.request_by_mail(@car, name, message).deliver
    redirect_to @car, :notice  => "Request sent."
  end

and the view where the form will be:

    <%= form_for :request_by_mail, :url => request_by_mail_car_path(@car), :html => {:method => :get} do |f| %>
                   <p>
                     <b>Your email:</b><br>
                     <%= f.text_field :your_name %>
                   </p>
                   <p>
                     <b>Details about your request:</b><br>
                     <%= f.text_area :your_message %>
                   </p>
                     <%= f.submit "Send details request" %>
                 <% end %>

now the e-mail template request_by_mail.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
  </head>
  <body>
    <h3>A client has requested details about car with stock number: <%= @car.id %></h3>

<% for asset in @car.assets %>
    <img alt="photos" src="http://localhost:3002<%= asset.asset.url(:thumb) %>">
<% end %>

<p><%= @name %></p>

<p><%= @message %></p>

    <p>
      Car link: <a href="http://localhost:3002/cars/<%= @car.id %>">Go to car</a>
    </p>
  </body>
</html>

Upvotes: 4

Related Questions