【问题标题】:mail_form gem for Contact Form联系表格的 mail_form gem
【发布时间】:2023-03-12 01:25:01
【问题描述】:

我想创建一个联系表单,我在某个论坛中找到了 mail_form gem。我尝试使用它,但没有让它工作。以下是我的代码 sn-p: 红宝石 1.8.7 导轨 2.3.15 mail_form 1.0.0

contact_form_controller.rb

require 'mail_form'

class ContactFormController < ApplicationController
  def new
    @contact_form = ContactForm.new
    respond_to do |format|
      format.html
      format.xml
    end
  end

  def create
    begin
      @contact_form = ContactForm.new(params[:contact_form])
      @contact_form.request = request
      if @contact_form.deliver
        flash.now[:notice] = 'Thank you for your message!'
      else
        render :new
      end
    end
  end

end

contact_form.rb 需要'mail_form'

class ContactForm < MailForm
  subject "My Contact Form"
  recipients "abc@gmail.com"
  sender{|c| %{"#{c.name}" <#{c.email}>}}


end

new.html.erb

<% form_for @contact_form do |f| %>
<%= f.error_messages %>
  <%= f.label :name %>
  <%= f.text_field :name %><br/>

  <%= f.label :email %>
  <%= f.text_field :email %><br/>

  <%= f.lable :message %>
  <%= f.text_area :message %><br/>

  <%= f.submit 'Submit' %>
<% end %>

任何建议都会对我有很大帮助。提前致谢。

【问题讨论】:

  • 您是否配置了smtp 或sendmail 设置?
  • hi @RajarshiDas sendmail 已配置并且正在工作。

标签: ruby-on-rails ruby mail-form


【解决方案1】:

根据文档,您的 ContactForm 应该是这样的:

class ContactForm < MailForm
  subject "My Contact Form"
  recipients "your.email@your.domain.com"
  sender{|c| %{"#{c.name}" <#{c.email}>} }

  attribute :name,      :validate => true
  attribute :email,     :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
  attribute :message
end

发件人:http://rubydoc.info/gems/mail_form/1.0.0/frames

【讨论】:

    猜你喜欢
    • 2015-06-10
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 2018-04-16
    相关资源
    最近更新 更多