如何获取我的Rails应用程序的基本URL(例如http:// localhost:3000)?

Way*_*ina 9 ruby-on-rails attachment paperclip

我正在使用Paperclip来允许用户附加内容,然后我发送电子邮件并希望将该文件附加到电子邮件中.我正在尝试读取该文件并将其添加为附件,如下所示:

# models/touchpoint_mailer.rb
class TouchpointMailer < ActionMailer::Base
  def notification_email(touchpoint)
    recipients "me@myemail.com"
    from "Touchpoint Customer Portal <portal@touchpointclients.com>"
    content_type "multipart/alternative"
    subject "New Touchpoint Request"
    sent_on Time.now
    body :touchpoint => touchpoint

    # Add any attachments the user has included
    touchpoint.assets.each do |asset|
      attachment :content_type => asset.file_content_type,
                 :body => File.read(asset.url)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误No such file or directory - /system/files/7/original/image.png?1254497688,堆栈跟踪说它是调用File.read.当我访问该show.html.erb页面,并单击图像链接时http://localhost:3000/system/files/7/original/image.png?1254497688,图像显示正常.

我该如何解决这个问题?

cwn*_*nja 20

通常root_url应提供此.

File.read期待文件路径,但不是url.如果要生成图像,则应调用图像生成代码并返回生成的图像的字节而不是调用File.read(…)


小智 4

asset.url返回文件的 URL。这通常是/system/classname/xx/xx/style/filename.ext. 你会把它放在一个image_tag.

你要asset.path。它返回文件的完整路径,通常类似于/home/username/railsapp/public/system/classname/xx/xx/style/filename.ext

HTH。