2011-09-18 70 views
1

我对我的资源之一有下载附件文件的操作。代码很简单Rails呈现忽略content_type散列值

def download 
    content_type = @book.attachments[params[:name]]['content_type'] 
    data = @book.read_attachment(params[:name]) 
    render :text => data, :content_type => content_type 
    end 

然而,rails完全忽略了这一点,并且无论如何设置了对text/html的响应。我甚至尝试使用response.header ['Content-Type']来设置它不起作用。

我使用rails 3.1,如果它很重要。

回答

2

请尝试使用send_data代替。

send_data data, :type => content_type 
+0

这使得它可以直接下载(而不是在一个浏览器页面打开),但类型仍然是text/html的 –

+0

好了,所以我觉得它的工作,一些与Chrome和“不修改”是导致它显示旧的内容类型。更改内容查询地址似乎有所帮助。 –