ruby-on-rails - Rails 3、Action Mailer、附件、form_for、电子邮件

标签 ruby-on-rails ruby-on-rails-3 actionmailer email-attachments

如何使用 form_for 将文件附加到邮件?到目前为止,它给了我一个错误:

UsersController#create 中的 ActiveRecord::StatementInvalid

NoMethodError: nil:NilClass: INSERT INTO "users"("created_at", "email", "file_field", "name", "text_field", "updated_at") VALUES (?, ? 、?、?、?、?)

这是我想出来的。

class UserMailer < ActionMailer::Base
  default :from => "example@gmail.com"

    def registration_confirmation(user)
      @user = user
         unless user[:file_field].nil?
           file=user[:file_field]
           attachments[file.original_filename] = File.open(file.path, 'rb'){|f| f.read}
         end
    mail(:to => user.email, :subject => "Registered")
  end
end

查看

  <%= form_for @user, :html => {:multipart => true} do |f| %>
<div class="field">
  <%= f.label :file_field %><br />
  <%= f.file_field :file_field %>
</div>
 <div class="field">
   <%= f.label :name %><br />
   <%= f.text_field :name %>
</div>
<div class="field">
   <%= f.label :email %><br />
   <%= f.text_field :email %>
</div>
<div class="actions">
   <%= f.submit %>
</div>
<% end %>

创建 Action

def create
@user = User.new(params[:user])

  if @user.save
UserMailer.registration_confirmation(@user).deliver
    redirect_to @user
  else
   render action: "new" 
  end
end

最佳答案

这是 stackoverflow 上的热门话题,一定要查看其他线程。它们非常深入,应该可以解决您的问题。

例如:Solution 1 , Solution 2 , Solution 3

如果没有结果,请回来,我们一定会启动另一个邮件附件线程。

关于ruby-on-rails - Rails 3、Action Mailer、附件、form_for、电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16125725/

相关文章:

ruby-on-rails-3 - Rails 中的日期字段和文本输入

ruby-on-rails - Rails 2.3.9 和 SendGrid,连接被拒绝 - 本地主机上的连接(2)

ruby-on-rails - Rails 邮件程序没有 View

ruby-on-rails - Delayed_job : Undefined method error on any call

ruby-on-rails - 创建自定义 move_up 路线和 move_down 操作/路线 Rails

ruby-on-rails - Rails 中的模型级授权

ruby-on-rails - rails : has_many through with polymorphic association - will this work?

ruby-on-rails - div类名称中的rails变量

ruby-on-rails - 如何在Rails 3中获取当前方法?

javascript - 如何在预览图像上添加复选框