什么是 utf8=✓&authenticity_token,为什么它会与 mail_form gem 产生冲突?

What is utf8=✓&authenticity_token and why is it creating conflicts with mail_form gem?

我正在使用 mail_form gem 和 sendgrid 作为我的电子邮件联系表。但是当我填写表格并发送消息时,它不会发送成功。通过发送消息,它应该将用户带到另一个页面,其中显示感谢您的消息。

我在我的 url 和 heroku 日志中得到这个 utf8=✓&authenticity_token。我认为这就是导致此问题的原因。你们能帮帮我吗?

2016-01-10T17:49:47.934925+00:00 app[web.1]: Started GET "/contacts/new?utf8=%E2%9C%93&authenticity_token=mzcayVlHayBSxoka6pnUQdjz7OxaPlFICU7L%2FJlfZU4NmyiLypTSbcgaJ%2BRSLZdmhYW3NaxMrZoL0Khwr%2FiRfA%3D%3D&contact%5Bname%5D=Brandon+Espinoza&contact%5Bemail%5D=espinozabrand%40gmail.com&contact%5Bmessage%5D=Hi+how+are+you+msg+me+back+asap+pls&contact%5Bnickname%5D=&commit=Yes%21+Send+It%21" for 71.9.177.97 at 2016-01-10 17:49:47 +0000
2016-01-10T17:49:47.950056+00:00 app[web.1]: Completed 200 OK in 10ms (Views: 9.1ms | ActiveRecord: 0.0ms)
2016-01-10T17:49:47.939506+00:00 app[web.1]: Processing by ContactsController#new as HTML
2016-01-10T17:49:47.939670+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"mzcayVlHayBSxoka6pnUQdjz7OxaPlFICU7L/JlfZU4NmyiLypTSbcgaJ+RSLZdmhYW3NaxMrZoL0Khwr/iRfA==", "contact"=>{"name"=>"Brandon Espinoza", "email"=>"espinozabrand@gmail.com", "message"=>"Hi how are you msg me back asap pls", "nickname"=>""}, "commit"=>"Yes! Send It!"}
2016-01-10T17:49:47.947225+00:00 app[web.1]:   Rendered contacts/new.html.erb within layouts/application (6.0ms)

这是我的 contacts_controller.rb:

class ContactsController < ApplicationController

  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(params[:contact])
    @contact.request = request
    if @contact.deliver
      flash.now[:error] = nil
    else
      flash.now[:error] = 'Cannot send message.'
      render :new
    end
  end

end

这是我的表格:

<div class="container">
  <div class="row">
    <div class="col-md-12">
      <h3 class="page-titles">Why Not Say Hello</h3>
    </div>
  </div>

  <div class="row">
    <div class="col-md-6 center-block">
      <form style="padding: 40px 0 40px 0;">
        <%= form_for @contact do |f| %>
          <div class="form-group">
            <%= f.label :name %>
            <%= f.text_field :name, required: true, class: "form-control"  %>
          </div>

          <div class="form-group">
            <%= f.label :email %>
            <%= f.email_field :email, required: true, class: "form-control" %>
          </div>

          <div class="form-group">
            <%= f.label :message %>
            <%= f.text_area :message, as: :text, class: "form-control", :cols => "30", :placeholder => "Your Message", :rows => "10" %>
          </div>

          <div class="form-group hidden">
            <%= f.label :nickname %>
            <%= f.text_field :nickname, hint: 'leave this field blank' %>
          </div>

          <%= f.submit 'Yes! Send It!', class: " btn-default btn sendbtn" %>

        <%end%>
      </form>
    </div>
  </div>
</div>

如果电子邮件发送成功,用户应该看到以下内容:

<div class="container">
  <div class="row">
    <div class="col-md-12">
      <h3 class="page-titles">Why Not Say Hello</h3>
    </div>
  </div>

  <div class="row">
    <div class="col-md-12">
      <h1>Thank you for your message!</h1>
      <p>I'll get back to you soon.</p>
    </div>
  </div>
</div>

这是我的 Production.rb:

config.action_mailer.default_url_options = { host: 'https://espinozabrandblog.herokuapp.com/' }
  config.action_mailer.delivery_method = :smtp

  ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],
  :password       => ENV['SENDGRID_PASSWORD'],
  :domain         => 'heroku.com',
  :enable_starttls_auto => true
}

这是我的路线:

Rails.application.routes.draw do

  resources :posts
  resources :projects
  resources :contacts, only: [:new, :create]
  get 'welcome/index'
  root 'welcome#index'
end

这应该可以回答您关于 utf8authenticity_token 字段的问题的第一部分:

Michael Hartl explains it like this:

This code, which isn’t displayed in the browser, is used internally by Rails, so it’s not important for us to understand what it does. Briefly, it uses the Unicode character ✓ (a checkmark ✓) to force browsers to submit data using the right character encoding, and then it includes an authenticity token, which Rails uses to thwart an attack called a cross-site request forgery (CSRF).

虽然我还没有使用这个 gem,你能启动一个控制台并在那里测试交付吗?这应该有效:

c = Contact.new
c.email = "your@email.com"
c.name = "Your Name"
c.message = "Test Subject"
c.deliver

更新:

您的日志没有显示任何错误。我知道你有问题:

  1. 邮件未发送。 您在使用控制台时收到电子邮件了吗? 如果这样做,请检查从表单传递的参数。查看强参数rails 4.提示:

如果没有,请再次按照我的控制台示例中的步骤操作,并按照 指出的那样查看 c.errors

  1. 用户在提交表单后没有收到 'Thank you'。 简单的方法是将 flash.now[:error] = nil 替换为 flash.now[:notice] = 'Thank you for your message!'。如果你想要一个专用页面,你需要那个页面、一个重定向和它附带的路由。

我找到了解决这个问题的方法。以上两种解决方案均无法解决。

出于某种原因,来自 Bootstrap 的表单标签造成了冲突。因此,只需从 Bootstrap 中取出 <form></form> 标签,即可提交表单。

所以表格应该是这样的

<div class="container">
  <div class="row">
    <div class="col-md-12">
      <h3 class="page-titles">Why Not Say Hello</h3>
    </div>
  </div>

  <div class="row">
    <div class="col-md-6 center-block">
        <%= form_for @contact do |f| %>
          <div class="form-group">
            <%= f.label :name %>
            <%= f.text_field :name, required: true, class: "form-control"  %>
          </div>

          <div class="form-group">
            <%= f.label :email %>
            <%= f.email_field :email, required: true, class: "form-control" %>
          </div>

          <div class="form-group">
            <%= f.label :message %>
            <%= f.text_area :message, as: :text, class: "form-control", :cols => "30", :placeholder => "Your Message", :rows => "10" %>
          </div>

          <div class="form-group hidden">
            <%= f.label :nickname %>
            <%= f.text_field :nickname, hint: 'leave this field blank' %>
          </div>

          <%= f.submit 'Yes! Send It!', class: " btn-default btn sendbtn" %>

        <%end%>
    </div>
  </div>