我正在使用Sequelize作为带有 express 的 ORM。
在Sequelize Model 中,它们的 is 字段接受空值。len如果提供了任何值,我想通过定义输入的长度来验证此字段。
代码:
field: {
type: DataTypes.TEXT,
allowNull: true,
validate: {
len: {
args: [50, 200],
msg: 'Please provide field within 50 to 200 characters.'
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当字段为空时,Sequelize 会抛出错误。那么,我如何允许空值,并且仅在提供值时才进行验证。
在rails中,我想使用带有从表单文件字段获取的附件的Action Mailer发送电子邮件,并希望通过sidekiq延迟它。
而且我写的代码如下。
鉴于:
<%= form_tag({ controller: 'my_controller', action: 'my_mail', method: 'post' }, { multipart: true }) do %>
<%= form_field_tag(:attachment) %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在控制器中:
def my_mail
MyMailer.delay.my_mail(params)
end
Run Code Online (Sandbox Code Playgroud)
在Mailer中:
def my_mail(message)
attachments['attachment'] = File.read(message[:attachment].tempfile)
mail(from: ENV['MY_MAIL'], to: ENV['MAIL_RECIVER'], subject: 'this is subject')
end
Run Code Online (Sandbox Code Playgroud)
但是,由于无法访问文件,将引发IOError。
而且,我在控制器中执行文件读取操作
def my_mail
MyMailer.delay.my_mail(File.read(params[:attachment].tempfile))
end
Run Code Online (Sandbox Code Playgroud)
现在,我可以在Mailer中添加附件为
attachments['attachment'] = message
Run Code Online (Sandbox Code Playgroud)
现在,它确实可以按我的要求工作,但是由于安全原因,在控制器中读取文件非常糟糕。
所以,现在我想知道附加从表单获取的文件并通过sidekiq发送文件的最佳方法。