Rails 3 - 包括View上的JavaScript? [英] Rails 3 - including JavaScript on a View?

查看:84
本文介绍了Rails 3 - 包括View上的JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我正在尝试做的事情....当用户登录并重定向到着陆页时,我想运行一些jquery ajax来抓取并注入一些内容。

here's what I'm trying to do right.... When a user signs in and is redirect to the landing page, I want to run a bit of jquery ajax to grab and inject some content.

我最初在我的application.js文件中有它,但问题是代码是那些没有登录的用户,这意味着我必须添加逻辑到帐户看起来不干净的东西。

I originally had it in my application.js file but the problem with that is that the code is there for user's that aren't logged in, which means I have to add logic to account for that which doesn't seem clean.

当调用视图以便能够包含JavaScript时,我想要的是...我尝试将其粘贴到视图文件本身但失败了,因为它是在页面底部的JQUERY之前加载的......

What I'd like is for when the view is called to be able to include JavaScript.... I tried pasting it in the view file itself but that failed because it is loaded before JQUERY which is at the bottom of the page...

Rails 3大师如何处理这个?谢谢!

How do Rails 3 gurus handle this? Thanks!

推荐答案

好吧,你可以在顶部包含jQuery文件。

Well, you could include the jQuery file at the top.

如果这不是一个选项,请在jQuery include下面的布局中添加<%= yield:javascript%> ,并执行:

If that's not an option, add <%= yield :javascript %> in the layout below the jQuery include, and do:

<% content_for :javascript do %>
    <script type="text/javascript">
        // code goes here
    </script>
<% end %>

它将被放置在布局中的适当位置。 (但是,出于缓存的考虑,我建议您在外部文件中编写Javascript并在必要时包含它而不是使用内联Javascript。只是很好的做法。)

It will be placed in the proper location in your layout. (However, I'd recommend, for caching's sake, that you write the Javascript in an external file and include it where necessary instead of using inline Javascript. Just good practice.)

这篇关于Rails 3 - 包括View上的JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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