Rails 3-如何在Rails中响应远程表单时发送send_file? [英] Rails 3 - how to send_file in response of a remote form in rails?

查看:127
本文介绍了Rails 3-如何在Rails中响应远程表单时发送send_file?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这张优惠券表格

    <%form_for(:download,:download,:url=>{:controller=>"coupons",:action=>"verifycoupon"},:remote=>true) do |f| %>
    <%=text_field :download,:code%>
    <%=f.submit "verify"%>
    <%end%>

并且在验证了控制器动作的代码后,我得到了如下确认:

and after validating the code on the controller's action i have a confirmation like:

render :update do |page|
    page.alert "OK"
end

现在我想使用send_file指令将文件发送到浏览器,但似乎什么也没发生

Now I want to send a file to the browser with the send_file instruction but nothing seems to happen

send_file("/path/to/my/file.extension")

在日志中我可以看到

"Sent file /path/to/my/file.extension (0.1ms)"

我想知道是否有类似的东西

I was wondering if there's something like

render :update do |page|
   page.send_file("/path/to/my/file.extension")
end

 

#### Update #######

我的控制器的动作看起来像

my Controller's action looks something like

def verifycoupon
   code = Code.find(params[:download][:code])
   if code
     if code.is_active?
     render :update do |page|
        page.alert "ok"
     end
       send_file("/path/to/my/file.extension")
     else
       render :update do |page|
          page.alert "this code has already been used"
       end
     end
   else
   render :update do |page|
     page.alert "Code does't exist"
   end
   end
end

推荐答案

我听说解决方案是将重定向发送到端点,该端点将send_file从Ajax返回.

I've heard that the solution is to send a redirect to an end-point where does the send_file back from Ajax.

因此,Ajax请求->服务器->响应->重定向->客户端->请求下载的文件->您停留在同一页面上.

So, Ajax Request -> Server -> Response -> Redirect -> Client -> request which downloads -> you stay on the same page.

def show
  # if javascript, then redirect to file_sender
end

def file_sender
  # Send file from here.
end

请参见 http://anaphoral.blogspot. com/2009/03/sendfile-or-senddata-in-linktoremote.html

这篇关于Rails 3-如何在Rails中响应远程表单时发送send_file?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆