滑轨的form_for用的是,file_field和远程=>真实,格式=> :JS [英] Rails form_for with file_field and remote => true and format => :js
问题描述
我提交与它的是,file_field一个的form_for。当表单提交的一个帖子,该控制器响应与将要执行的.js文件。
的是,file_field是可选的。
当我上传的文件,JS文件是在一个新的文件打开(如一个简单的文本文件),不执行JS。当我不上传任何文件,响应格被更新。我想更新成功的响应格或独立文件上传失败消息通知。
可能是这样做了一些安全方面的原因?有一些解决办法的人谁希望能够做到这一点?
粗略地说,
窗体部分_action.html.erb:
< DIV ID =响应>< / DIV>
<%=的form_for ...:远程=>如此,:URL => {...:格式=> :JS}办| F | %>
...
<%= f.file_field:名称%>
<%结束%GT;
控制器::行动
如果不request.POST的?
渲染:部分=> 控制器/行动
返回
其他
@response =保存成功;
#呈现action.js.erb
结束
action.js.erb:
$(#响应)HTML(<%= @response%>中)。
尝试使用remotipart宝石的https:// github上.COM / JangoSteve / remotipart
I am submitting a form_for with a file_field in it. When the form is submitted with a post, the controller responds with a .js file which will be executed.
The file_field is optional.
When I upload a file, the js file is opened in a new document (as a simple text file) and the js is not executed. When I don't upload any file, the response div is updated. I want the update success to be notified in the response div or a failure message independent of the file upload.
May be this is done for some security reasons? Is there some workaround for people who want to be able to do this?
Roughly,
Form partial _action.html.erb:
<div id="response"></div>
<%= form_for ... :remote => true, :url => {..., :format => :js} do |f| %>
...
<%= f.file_field :name %>
<% end %>
Controller::action
if not request.post?
render :partial => 'controller/action'
return
else
@response = "save was successful";
# renders the action.js.erb
end
action.js.erb:
$("#response").html("<%= @response %>");
Try to use remotipart gem https://github.com/JangoSteve/remotipart
这篇关于滑轨的form_for用的是,file_field和远程=&GT;真实,格式=&GT; :JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!