简体   繁体   English

Rails Controller Action被两次调用

[英]Rails Controller Action being called twice

In my rails app, I have a link so that a user can download a GIF on the site: 在我的rails应用程序中,我有一个链接,以便用户可以在网站上下载GIF:

<%= link_to "gif", :controller => "projects", :action => :export_gif, :id => @project.id %>

This is the corresponding controller action: 这是相应的控制器操作:

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

When the user clicks on the link, the export_gif action is being called twice. 当用户单击链接时,export_gif操作将被调用两次。 How do I ensure that it only gets called once? 如何确保只调用一次?

Here's what the logs look like after I click the link: 单击链接后,日志如下所示:

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)

For me it was because of <img src="#"> . 对我来说是因为<img src="#"> I had this in one of the files of layouts. 我将其放在布局文件之一中。
So I changed "#" with " http://example.com " and issue got resolved. 因此,我用“ http://example.com ”更改了“#”,问题得到解决。

I got this solution from here(aNoble's answer): 我从这里得到了这个解决方案(aNoble的答案):
Rails seems to be serving the page twice Rails似乎两次为该页面提供服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM