使用sendGrid的Action Mailer配置困难

时间:2018-03-20 12:55:14

标签: html ruby-on-rails forms sendgrid

我正在构建一个rails web application,其中一个表单是用纯HTML完成的。

我无法连接表单,以便应用程序使用Sendgrid通过电子邮件向我发送提交的字段。

当我点击表单上的提交按钮时,我收到无路由匹配[POST]" /"

我已遵循此tutorial并按时间顺序执行说明,但我仍然很烦。任何指导或帮助代码将不胜感激。

配置/环境/ production.rb:

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_startstls_auto => true
  } 

index.html.erb

<form  method="post" name="frmContentDownload"><!-- /. form handling starts here-->

      <div>
        <label for="FirstName1">First Name</label><input class="form-control" name="FirstName1" type="text" value="" />
      </div>

      <div>
        <label for="LastName1">Last Name</label><input class="form-control" name="LastName1" type="text" value="" />
      </div>

      <div>
        <label for="emailAddress">Email</label><input class="form-control" name="emailAddress" type="text" value="" />
      </div>

      <div>
        <label for="PhoneNumber">Phone</label><input class="form-control" name="PhoneNumber" type="text" value="" />
      </div>

      <div>
        <label for="Title1">Title</label><input class="form-control" name="Title1" type="text" value="" />
      </div>


      <!-- /. this is the drop down box for the states-->

      <div><label for="StateProvince1">State or Province</label><select class="form-control" name="StateProvince1">
        <option class="placeholder" value="" disabled selected>COUNTRY &#8609;</option>
        <option value="CA">Canada</option>
        <option value="UK">United Kingdom</option>
        <option value="US">United States</option>
      </select></div>

        <div>
          <label for="comments">Comments</label><input class="form-control" name="comments" type="text" value="" />
        </div>

        <div>
          <input class="btn btn-primary btn-lg btn-block" type="submit" value="Contact Us" />
        </div>

</form>

message.rb

class Message < ApplicationRecord

    include ActiveModel::Model
    include ActiveModel::Conversion
    include ActiveModel::Validations

    attr_accessor :FirstName1, :LastName1, :emailAddress, :PhoneNumber, :Title1, :StateProvince1, :comments

    validates :FirstName1,
       presence: true

    validates :LastName1,
       presence: true


    validates :emailAddress,
       presence: true


    validates :PhoneNumber,
       presence: true


    validates :Title1,
       presence: true


    validates :StateProvince1,
       presence: true


    validates :comments,
       presence: true

end

邮件程序类

class ContactMailer < ApplicationMailer
    default from: 'notifications@example.com'
    default to: 'chrisdorman1978@gmail.com'

    def welcome_email(user)
        @user = user
        @url = 'http://example.com/login'
        mail(to: @user.email, subject: 'Thank you for the enquiry, Gastromend will be in touch')
    end

    def new_message(message)
        @message = message

        mail subject: "Message from #{message.name}"
    end 

end

控制器

class MessagesController < ApplicationController


    def new
       @message = Message.new
    end


    def create
       @message = Message.new(message_params)

       if @message.valid?
         ContactMailer.new_message(@message).deliver
         redirect_to contact_path, notice: "Your messages has been sent."
       else
         flash[:alert] = "An error occurred while delivering this message."
         render :new
       end
    end 


    private

    def message_params
       params.require(:message).permit(:FirstName1, :LastName1, :emailAddress, :PhoneNumber, :Title1, :StateProvince1, :comments)
    end

end

的routes.rb

Rails.application.routes.draw do

root :to => "pages#index"   
  get 'pages/index'

  get 'contact', to: 'messages#new', as: 'contact'
  post 'contact', to: 'messages#create'

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

new_message.text.erb

<%= @message.name %> <<%= @message.email %>> wrote:


<%= @message.content %>

1 个答案:

答案 0 :(得分:1)

看,如果您在控制台上键入rake routes,则会显示

contact GET    /contact(.:format)          messages#new
        POST   /contact(.:format)          messages#create

这意味着您的帖子操作网址为/contact,那么您的form标记将为

<form action="/contact" method="post" name="frmContentDownload">

另外根据rails form helper

为您撰写

为什么你不使用Rails表单标签,看看你是否使用 Rails 表单标签,那么它就像这样

<%= form_tag mail_send_path, method: :post do %>
      <div>
        <label for="FirstName1">First Name</label>
        <%= text_field_tag :FirstName1, nil, class: "form-control", placeholder: "First Name" %>
      </div>

      <div>
        <label for="LastName1">Last Name</label>
        <%= text_field_tag :LastName1, nil, class: "form-control", placeholder: "Last Name" %>
      </div>

      <div>
        <label for="emailAddress">Email</label>
        <%= email_field :emailAddress, nil, class: "form-control", placeholder: "Email" %>
      </div>

      <div>
        <label for="PhoneNumber">Phone</label>
        <%= text_field_tag :PhoneNumber, nil, class: "form-control", placeholder: "PhoneNumber" %>
      </div>

      <div>
        <label for="Title1">Title</label>
        <%= text_field_tag :Title1, nil, class: "form-control", placeholder: "Title1" %>
      </div>


      <!-- /. this is the drop down box for the states-->

      <div><label for="StateProvince1">State or Province</label>
        <%= select_tag(:StateProvince1, '<option value="CA">Canada</option><option value="UK">United Kingdom</option><option value="US">United States</option>') %>
      </div>

        <div>
          <label for="comments">Comments</label>
          <%= text_field_tag :comments, nil, class: "form-control", placeholder: "comments" %>
        </div>

        <div>
          <input class="btn btn-primary btn-lg btn-block" type="submit" value="Contact Us" />
        </div>
<% end %>

你的路线文件将是这样的

联系&#39;,以及:&#39;消息#new&#39;,as:&#39; contact&#39;   发布&#39;联系&#39;至:&#39;消息#create&#39;,as:&#39; mail_send&#39;

然后现在如果您将运行rake routes,那么它将显示为

contact GET    /contact(.:format)          messages#new
mail_send POST   /contact(.:format)          messages#create

现在mail_send是您的表单发布请求路径