我有以下联系表格,我想允许客户将图像作为附件发送给我。我已将字段添加到表单中,但不了解以下内容 -
<%= simple_form_for @inquiry, :method => :post do |f| %>
<%= f.input :spam, as: :hidden %>
<%= f.input :name %>
<%= f.input :phone %>
<%= f.input :email %>
<%= f.input :message %>
<%= f.file_field :image %> ## Attachment input here
<%= f.button :submit, 'Send' %>
<% end %>
def new
@inquiry = Inquiry.new
end
def create
redirect_to new_inquiry_path and return if params[:spam].present?
@inquiry = Inquiry.new(inquiry_params)
if @inquiry.valid?
InquiryMailer.admin(@inquiry).deliver
redirect_to inquiries_path
else
render :new
end
end
private
def inquiry_params
params.require(:inquiry).permit(:name, :email, :phone, :message)
end
validates :name, presence: true
validates :email, presence: true
validates :phone, presence: true
validates :message, presence: true
class InquiryMailer < ActionMailer::Base
default from: "noreply@foo.com"
def admin(inquiry)
@inquiry = inquiry
mail to: "hello@foo.co.uk", subject: "Website Inquiry"
end
end
Website Inquiry
=========
Name: <%= @inquiry.name %>
Phone: <%= @inquiry.name %>
E-Mail: <%= @inquiry.email %>
Message: <%= @inquiry.message %>
答案 0 :(得分:2)
您可以在此处使用ActionMailer
我会根据您现有的设置提供答案
您需要编辑邮件以查找此类内容
class InquiryMailer < ActionMailer::Base
default from: ENV['EMAIL_ADDRESS'] #I like to keep my email address hidden
def admin(inquiry)
@inquiry= inquiry
if inquiry.file
attachment_name = inquiry.file.original_filename
attachments[attachment_name] = inquiry.file.read
end
mail(to: ENV['EMAIL_ADDRESS'], subject: 'Website Inquiry')
end
end
您可以保持admin.text.erb不变,并且您的控制器保持不变
并在您的form_for中忘记添加
<%= simple_form_for @inquiry, :method => :post, :multipart => true do |f| %>
因此您可以添加附件
认为这是我的头脑
希望有帮助,有任何问题请问