Rails 3 邮件程序从表单获取数据

发布于 2024-12-10 09:24:03 字数 513 浏览 0 评论 0原文

我有一个电子邮件操作..简单的链接:

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

这个应该向管理员邮件发送一条通知,告知某些客户正在为这辆车提供特定金额的资金。因此,客户必须在表格中插入他的电子邮件和报价。这些数据不存储在数据库中,仅用于发送电子邮件和清除。因此,目前我确实收到了包含汽车数据、名称、图片网址等的电子邮件。但是我如何构建一个表单来显示这两个字段以及客户电子邮件和报价、控制器的外观以及链接本身。感谢您宝贵的时间。

控制器:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无需解释 2024-12-17 09:24:03

感谢我的一位朋友,我得到了答案。我将在这里发布解决方案,因为可能有人也需要这个。

在汽车邮件程序中,将

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

在 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

进行操作,并且视图中的表单将是:

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

现在是电子邮件模板 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文