在我的rails应用程序中,我有一个链接,以便用户可以在网站上下载GIF:
<%= link_to "gif", :controller => "projects", :action => :export_gif, :id => @project.id %>
这是相应的控制器操作:
def export_gif
if @project.gif.blank?
@project.generate_gif #this creates @project.gif
end
gif_path = @project.gif.gif_file_url
gif_path.sub! 'https', 'http'
send_data open(gif_path).read, filename: "project_#{@project.id}.gif", type: "image/gif"
end
当用户点击链接时,将调用export_gif操作两次。我如何确保只调用一次?
点击链接后,这里的日志是什么样的:
Started GET "/projects/38/export_gif" for ::1 at 2015-06-16 17:08:55 -0400
Processing by ProjectsController#export_gif as HTML
Parameters: {"id"=>"38"}
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1 [["id", 38]]
Gif Load (0.1ms) SELECT "gifs".* FROM "gifs" WHERE "gifs"."project_id" = ? LIMIT 1 [["project_id", 38]]
Rendered text template (0.0ms)
Sent data project_38.gif (3.6ms)
Completed 200 OK in 207ms (Views: 3.4ms | ActiveRecord: 0.2ms)
Started GET "/projects/38/export_gif" for ::1 at 2015-06-16 17:08:55 -0400
Processing by ProjectsController#export_gif as HTML
Parameters: {"id"=>"38"}
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1 [["id", 38]]
Gif Load (0.1ms) SELECT "gifs".* FROM "gifs" WHERE "gifs"."project_id" = ? LIMIT 1 [["project_id", 38]]
Rendered text template (0.0ms)
Sent data project_38.gif (0.6ms)
Completed 200 OK in 196ms (Views: 0.5ms | ActiveRecord: 0.1ms)
答案 0 :(得分:0)
对我来说,这是因为<img src="#">
。我在其中一个布局文件中有这个
所以我改变了#34;#&#34;用&#34; http://example.com&#34;问题得到解决。
我从这里得到了这个解决方案(aNoble的回答):
Rails seems to be serving the page twice