我想重定向到用户上传的pdf。
因此,在浏览器中,当我转到/news_ pdf /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时,它会为回形针::附件:类指定未定义的方法'model_name‘。
这是为什么?
谢谢您:)
发布于 2013-09-23 17:39:42
这样做的方法是在news_article的控制器中使用def show编写redirect_to @news_article.attach.url。
在news_article回形针商店中,附件中的url (我在模型中将其称为url)和.url帮助器使浏览器在那里运行:)
发布于 2013-09-17 16:17:59
看起来@news_article.attach正在将回形针::附件对象返回给您。由于耳聋to_s方法的回形针::附件类是url,您看到的pdf路径时,单独评估它。但实际上它不是“字符串”路径。因此,在重定向调用中,通过显式调用@news_article.attach上的redirect_to @news_article.attach.to_s,尝试提供字符串路径。
https://stackoverflow.com/questions/18854385
复制相似问题