如何将 Rails 变量传递给 jQuery 函数?

发布于 2024-10-04 14:54:33 字数 717 浏览 7 评论 0原文

我正在制作非常简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

GRAY°灰色天空 2024-10-11 14:54:33

保持 Rails 代码不变并在 jQuery 中使用:

$(document).ready(function(){
  $('#player').youTubeEmbed($('#random').text())
});

为了增强可访问性,我将 erb 更改为:

<div id="random">
<%=link_to h(@clip), h(@clip) %>
</div>

然后是 js:

$(document).ready(function(){
  $('#player').youTubeEmbed($('#random a').attr('href'));
  $('#random').hide();
});

Leave your rails code as is and in jQuery use:

$(document).ready(function(){
  $('#player').youTubeEmbed($('#random').text())
});

For enhanced accessibility I'd change the erb to:

<div id="random">
<%=link_to h(@clip), h(@clip) %>
</div>

and then the js:

$(document).ready(function(){
  $('#player').youTubeEmbed($('#random a').attr('href'));
  $('#random').hide();
});
放我走吧 2024-10-11 14:54:33

假设变量包含 URL,如下所示:

$(document).ready(function(){
    $('#player').youTubeEmbed("<%=escape_javascript(@clip)%>");
});

(抱歉,忽略了 JavaScript 与 HTML 不在同一个文件中的事实 - 以及它不应该是!)

假设变量包含 URL,如果您不希望显示 URL,您可以将其嵌入到页面变量中,如下所示(替换 div 代码在 show.html.erb 中包含以下内容):

<script type='text/javascript'>
window.myStuff = {
    clipUrl: "<%=escape_javascript(@clip)%>"
};
</script>

...(或

<script type='text/javascript'>
var myStuff = {
    clipUrl: "<%=escape_javascript(@clip)%>"
};
</script>

...几乎是同一件事)。

...然后你可以像这样使用它:

$(document).ready(function(){
    $('#player').youTubeEmbed(window.myStuff.clipUrl);
});

当以这种方式输出变量的值时,你需要确保写出的内容最终是有效的 JavaScript。例如,如果 @clip 变量包含 "\,您需要确保 "< /code> 被转换为 \" 并且任何单独的反斜杠都被转换为 \\。 Jakub Hammpl 很有帮助地指出了 escape_javascript 函数,我已将其编辑到代码中 这样做意味着

我们要在 window 上放置一个新符号,这样如果我们需要对其他东西执行此操作,我们可以将它们包含在同一个对象中。所以我们最终不会得到到处都是符号的结果(创建大量全局符号——也就是说,window属性——往往会成为维护问题,最好避免)。例如,如果您有两个剪辑:

<script type='text/javascript'>
window.myStuff = {
    someNiftyClip:      "<%=escape_javascript(@clip)%>",
    someOtherNiftyClip: "<%=escape_javascript(@anotherClip)%>"
};
</script>

偏离主题:在调用youTubeEmbed的行中,请注意我在末尾添加了一个分号。最好不要依赖 JavaScript 的分号插入

Assuming that the variable contains the URL, like this:

$(document).ready(function(){
    $('#player').youTubeEmbed("<%=escape_javascript(@clip)%>");
});

(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):

<script type='text/javascript'>
window.myStuff = {
    clipUrl: "<%=escape_javascript(@clip)%>"
};
</script>

...(or

<script type='text/javascript'>
var myStuff = {
    clipUrl: "<%=escape_javascript(@clip)%>"
};
</script>

...which is very nearly the same thing).

...and then you can use it like this:

$(document).ready(function(){
    $('#player').youTubeEmbed(window.myStuff.clipUrl);
});

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 the escape_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:

<script type='text/javascript'>
window.myStuff = {
    someNiftyClip:      "<%=escape_javascript(@clip)%>",
    someOtherNiftyClip: "<%=escape_javascript(@anotherClip)%>"
};
</script>

Off-topic: In the line calling youTubeEmbed, note I added a semicolon at the end. Best not to rely on JavaScript's semicolon insertion.

喜爱纠缠 2024-10-11 14:54:33

您可以将变量放入隐藏字段中,然后可以在 $('#hiddenFieldID').val(); 等函数中使用它

You could put the variable in a hidden field, then you could use it in the function like $('#hiddenFieldID').val();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文