Rails:File.read中的路径

时间:2013-07-17 09:19:14

标签: ruby-on-rails actionmailer

在user_mailer.rb

def welcome_email(message)
@message = message   
@image=@message.pics_file_name  #Getting file name
attachments['@image']=File.read('path/@image') #Have to get path here
mail(:to => @message.mailto, :subject => 'Welcome to my site')
end

我正在从数据库中检索文件名。我必须使用File.read()中的文件名将文件作为附件发送到指定的邮件

2 个答案:

答案 0 :(得分:3)

试试这个

def welcome_email(message)
@message = message   
@image=@message.pics_file_name  #Getting file name

#you need to give application root path here 
attachments[@image]=File.read("#{Rails.root}/your assets path/#{@image}") #Have to get path here
mail(:to => @message.mailto, :subject => 'Welcome to my site')
end

您的资产路径可能是,如果您将资产存储在公共目录中,那么路径将是

attachments[@image]=File.read("#{Rails.root}/public/assets/#{@image}")

答案 1 :(得分:1)

如果你的目录结构如下:

 App base Directory
   |
   |-> mail_resources
        |
        | -> images
              |
              |-> image_file

然后你可以使用

Rails.root.join("mail_resources", "images", "image_file")