0

我想重定向到用户上传的 pdf。

所以在浏览器中,当我转到 /news_article/3 时,我希望它显示 pdf,pdf 的路径存储在数据库中,我该如何重定向到它?

在我的控制器中的表演动作中@news_article = NewsArticle.find(params[:id])

模型

class AddAttachPaperclipToNewsArticles < ActiveRecord::Migration
  def change
    add_column :news_articles, :attach_file_name, :string
    add_column :news_articles, :attach_content_type, :string
    add_column :news_articles, :attach_file_size, :integer
    add_column :news_articles, :attach_updated_at, :datetime
  end
end

如果我这样做@news_article.attach,它会给我 pdf 的路径,但我怎样才能让浏览器显示 pdf?当我尝试redirect_to @news_article.attach在控制器中时,它显示回形针::附件:类的未定义方法“模型名称”。

为什么是这样?

谢谢 :)

4

2 回答 2

1

The way to do this is in the controller for the news_article in def show you write redirect_to @news_article.attach.url

in news_article paperclip store url in attach (I call it this in model) and the .url helper is what makes the browser go there :)

于 2013-09-23T17:39:42.340 回答
0

看起来@news_article.attach 正在将回形针::attachment 对象返回给您。由于 paperclip::attachment 类的默认 to_s 方法是 url,当单独评估它时,您会看到 pdf 路径。但实际上它不是“字符串”路径。因此,在重定向调用中,尝试通过在 @news_article.attach 上显式调用 to_s 来给出字符串路径,例如 redirect_to @news_article.attach.to_s

于 2013-09-17T16:17:59.320 回答