我有两个使用 mail_form 构建的联系表格。一个有效,一个无效,即使从我的角度来看它们是相同的

I have two contact forms build with mail_form. One works, one not, even if from my point of view they are identically written

我已经用 mail_form gem 建立了联系表。我有两个不同的联系人需要通过两封不同的邮件接收消息。所以我已经完成了 contactsmessages 控制器以及 contactmessage 模型。我可以使用 联系人表单 发送电子邮件,但我不能使用 消息表单 .

只有当我尝试使用 消息格式 :

发送电子邮件时,我才会从服务器收到此消息
Started POST "/messages" for 127.0.0.1 at 2020-07-20 10:11:27 +0200
Processing by MessagesController#create as HTML
  Parameters: {"authenticity_token"=>"lQKACWLv72xM6tjq9wvpWNbKZ65QMpdXpnrrGIIemvfFmPfAEOoDtQN/
qUSjU7lZdhTHcqKEPMblhf5zHUtgRg==", "message"=>{"name"=>"me", "email"=>"email.email@gmail.com",
 "message"=>"Hello", "nickname"=>""}, "commit"=>"Send Message"}
  Rendering messages/new.html.erb within layouts/application
  Rendered messages/new.html.erb within layouts/application (Duration: 2.7ms | Allocations: 959)
[Webpacker] Everything's up-to-date. Nothing to do

而且我不知道如何检查服务器显示的错误消息....

因此发送电子邮件:

app/controllers/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
      redirect_to root_path, notice: 'Message sent successfully'
    else
      flash.now[:error] = 'Cannot send message'
      render :new
    end
  end
end

app/modles/contact.rb

class Contact < MailForm::Base
  attribute :name, :validate => true
  attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :message,   :validate => true
  attribute :nickname,  :captcha  => true

  def headers
    {
        :subject => "Contact Form",
        :to => "email.email@gmail.com",
        :from => %("#{name}" <#{email}>)
    }
  end
end

app/views/contacts/new.html.erb

<%= form_for @contact do |f| %>
  <% if flash[:error].present? %>
    <p> flash[:error]</p>
  <% end %>
  <%= f.label :name %> <br>
  <%= f.text_field :name, required: true %>

  <br>
  <%= f.label :email %> <br>
  <%= f.text_field :email, required: true %>

  <br>
  <%= f.label :message %> <br>
  <%= f.text_area :message, as: :text %>


  <%= f.label :nickname %> <br>
  <%= f.text_field :nickname, hint: 'leave this field blank' %>


  <%= f.submit 'Send Message', class: "button" %>
<% end %>

以及不发送邮件的:

app/controllers/messages_controller.rb:

class MessagesController < ApplicationController
  def new
    @message = Message.new
  end

  def create
    @message = Message.new(params[:contact])
    @message.request = request
    if @message.deliver
      flash.now[:error] = nil
      redirect_to root_path, notice: 'Message sent successfully'
    else
      flash.now[:error] = 'Cannot send message'
      render :new
    end
  end
end

app/models/message.rb:

class Message < MailForm::Base
  attribute :name, :validate => true
  attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :message,   :validate => true
  attribute :nickname,  :captcha  => true

  def headers
    {
        :subject => "Contact Form",
        :to => "email@gmail.com",
        :from => %("#{name}" <#{email}>)
    }
  end
end

app/views/messages/new.html.erb:

<%= form_for @message do |f| %>
  <% if flash[:error].present? %>
    <p> flash[:error]</p>
  <% end %>
  <%= f.label :name %> <br>
  <%= f.text_field :name, required: true %>

  <br>
  <%= f.label :email %> <br>
  <%= f.text_field :email, required: true %>

  <br>
  <%= f.label :message %> <br>
  <%= f.text_area :message, as: :text %>


  <%= f.label :nickname %> <br>
  <%= f.text_field :nickname, hint: 'leave this field blank' %>


  <%= f.submit 'Send Message', class: "button" %>
<% end %>

在您的 MessagesController 中,您需要一个名为 contact

的参数

改成这样:

def create
  @message = Message.new(params[:message])

而不是 :contact 作为预期参数。

应该可以。