如何将 Rails 变量传递给 jQuery 函数?
我正在制作非常简单的 ROR (rails 2.3.5) 应用程序并使用 jQuery 插件 jRails。这个想法是在 jQuery youtube 播放器中进行修改,它将播放 ror 控制器列表中的随机视频。
controller.rb
def show
@clip = Video.find(:all, :select =>'link', :order => 'RANDOM()', :limit => 1).map(&:link)
respond_to do |format|
format.html
end
end
show.html.erb
<div id="random">
<%=@clip%>
</div>
这里的链接只是一个随机的 YouTube 链接。对我来说,问题是如何在这个 jQuery 函数中获取这个变量?
application.js
$(document).ready(function(){
$('#player').youTubeEmbed("http://www.youtube.com/watch?v=ZDFlmxmBne8")
});
非常感谢您的帮助。
I'm making very simple ROR (rails 2.3.5) application and using jQuery plugin jRails. The idea is to lunch modified in jQuery youtube player, which will play random videos from ror's controller's list.
controller.rb
def show
@clip = Video.find(:all, :select =>'link', :order => 'RANDOM()', :limit => 1).map(&:link)
respond_to do |format|
format.html
end
end
show.html.erb
<div id="random">
<%=@clip%>
</div>
The link here is just a random youtube link for example. The question for lame me is how to get this variable in this jQuery function?
application.js
$(document).ready(function(){
$('#player').youTubeEmbed("http://www.youtube.com/watch?v=ZDFlmxmBne8")
});
Would be very thankful for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
保持 Rails 代码不变并在 jQuery 中使用:
为了增强可访问性,我将 erb 更改为:
然后是 js:
Leave your rails code as is and in jQuery use:
For enhanced accessibility I'd change the erb to:
and then the js:
假设变量包含 URL,如下所示:(抱歉,忽略了 JavaScript 与 HTML 不在同一个文件中的事实 - 以及它不应该是!)假设变量包含 URL,如果您不希望显示 URL,您可以将其嵌入到页面变量中,如下所示(替换
div
代码在 show.html.erb 中包含以下内容):...(或
...几乎是同一件事)。
...然后你可以像这样使用它:
当以这种方式输出变量的值时,你需要确保写出的内容最终是有效的 JavaScript。例如,如果
@clip
变量包含"
或\
,您需要确保"< /code> 被转换为
\"
并且任何单独的反斜杠都被转换为\\
。 Jakub Hammpl 很有帮助地指出了escape_javascript
函数,我已将其编辑到代码中 这样做意味着我们要在
window
上放置一个新符号,这样如果我们需要对其他东西执行此操作,我们可以将它们包含在同一个对象中。所以我们最终不会得到到处都是符号的结果(创建大量全局符号——也就是说,window
属性——往往会成为维护问题,最好避免)。例如,如果您有两个剪辑:偏离主题:在调用
youTubeEmbed
的行中,请注意我在末尾添加了一个分号。最好不要依赖 JavaScript 的分号插入。Assuming that the variable contains the URL, like this:(Sorry, missed the fact that the JavaScript was not in the same file as the HTML -- as well it shouldn't be!)Assuuming the variable contains the URL, if you don't want the URL shown, you can embed it in a page variable like so (replace the
div
code in show.html.erb with the following):...(or
...which is very nearly the same thing).
...and then you can use it like this:
When outputting a variable's value in this way, you need to ensure that what gets written out ends up being valid JavaScript. So for instance, if the
@clip
variable contains a"
or a\
, you'll need to ensure that the"
is turned into\"
and that any lone backslash is turned into\\
. Jakub Hampl helpfully pointed out theescape_javascript
function for this, which I've edited into the code examples.Doing this that means we're putting a new symbol on
window
. I made our new symbol an object so that if we need to do this with other things, we can include them in that same object so we don't end up with symbols all over the place (creating lots of global symbols — which is to so,window
properties — tends to become a maintenance issue, best avoided). So for instance, if you had two clips:Off-topic: In the line calling
youTubeEmbed
, note I added a semicolon at the end. Best not to rely on JavaScript's semicolon insertion.您可以将变量放入隐藏字段中,然后可以在 $('#hiddenFieldID').val(); 等函数中使用它
You could put the variable in a hidden field, then you could use it in the function like $('#hiddenFieldID').val();