Rails 3和jQuery-如何将Ruby代码添加到元素中 [英] Rails 3 and jQuery - How to add Ruby code into an element
问题描述
我正在尝试使用jQuery,jQuery表单插件和Ruby on Rails创建AJAX图片上传器.
I'm trying to use jQuery, jQuery form plugin, and Ruby on Rails to create an AJAX image uploader.
我已经完成了所有图像的上传和ActiveRecord的创建,但是AJAX部件本身不起作用-它会导致<div>
重新呈现Picture局部图像的集合.但是,我尝试插入的Ruby代码被呈现为字符串,而不是被执行.
I've got all the image uploading and ActiveRecord creating done, but the AJAX part itself won't work - it should cause a <div>
to re-render a collection of Picture partials. However, the Ruby code I'm trying to insert is being rendered as a string instead of being executed.
application.js:
$(document).ready(function(){
$("#upload input").change(function(event){
$(this).parent().ajaxSubmit({
beforeSubmit: function(a,f,o) {
o.dataType = 'json';
},
complete: function(XMLHttpRequest, textStatus) {
$("#img_div").html('<%= escape_javascript(render(@picture.idea.pictures)) %>');
},
});
});
});
当文件字段更改时,将表单绑定到ajaxSubmit,效果很好. .html()似乎是有问题的,因为字符串<%= escape_javascript(render(@picture.idea.pictures)) %>
仅打印在<div>
中.
Binds the form to ajaxSubmit when the file field is changed, which works fine. The .html() is what seems to have the problem, as the string <%= escape_javascript(render(@picture.idea.pictures)) %>
is simply printed in the <div>
.
_form.html.erb:
<div id="img_div"><%= render @picture.idea.pictures %></div>
该行在页面刷新上可以很好地呈现集合.
That line works fine on page refresh to render the collection.
我知道Ruby代码没有问题,因为我以前在 create.js.erb 文件中有该行.我将继续使用该方法,除了现在使用ajaxSubmit之外,我无法将create动作呈现为JS-始终以HTML呈现.
And I know there's not a problem with the Ruby code because I previously had that exact line in a create.js.erb file. I would continue to use that approach, except now that I am using ajaxSubmit, I can't get the create action to be rendered as JS - it's always rendered as HTML.
因此,从理论上讲,我可以通过以下两种方法之一解决问题-找出为什么将ruby代码在 application.js 中呈现为字符串,或者将pictures#create
呈现为JS.
So in theory I can solve the problem one of two ways - figure out why the ruby code is rendered as a string in application.js, or get pictures#create
to be rendered as JS.
我为那些事情不会发生做错了什么吗?
Am I doing something wrong for those things to not happen?
好,所以看来我根本无法在 applications.js 中使用Ruby代码或变量,并且尝试将Ruby集合传递到JavaScript中是行不通的,因为那样我就可以无法访问我需要重新创建正确的HTML的所有属性.
Ok, so it seems like I can't use Ruby code or variables at all in applications.js, and trying to pass the Ruby collection into JavaScript doesn't work because then I can't access all the attributes I need to recreate the correct HTML.
我需要的是一种确保pictures#create
操作被称为JS的方法.然后,我可以使用format.js
来确保使用 create.js.erb (在我知道我的代码可以正常工作的地方).有什么建议吗?
What I need is a way to ensure that the pictures#create
action is called as JS. Then I can use format.js
to ensure that create.js.erb is used, where I know my code works. Any advice?
推荐答案
如果视图中的脚本中包含红宝石代码,则只能将其嵌入到javascript中. public/javascripts/
中的任何内容都永远不会命中服务器,因此无法找到ruby变量.
You can only embed ruby code in javascript if it is in a script in the view. Anything in public/javascripts/
never hits the server so it cannot find the ruby variables.
您要做的是在包含脚本之前将ruby变量存储在javascript变量中.
What you want to do is store the ruby variable in a javascript variable just before you include your scripts.
<head>
<script type="text/javascript">
var youJsVariable = <%= ruby_array %>;
</script>
<%= javascript_include_tag :defaults, 'application' %>
</head>
通过这种方式,您不必编写很多内联脚本,这些脚本可以使那些不引人注意的人摆脱烦恼.
This way you don't have to write a lot of inline scripts which bug the heck out of the unobtrusive folks.
这篇关于Rails 3和jQuery-如何将Ruby代码添加到元素中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!