简体   繁体   English

在Ruby中使用Javascript

[英]Using Javascript in Ruby

In my Rails project, I have a file /app/assets/javascripts/bootstrap-tab.js which allows me to use tabs in my Rails project, which uses Twitter Bootstrap HTML. 在我的Rails项目中,我有一个文件/app/assets/javascripts/bootstrap-tab.js ,该文件使我可以在Rails项目中使用选项卡,该项目使用Twitter Bootstrap HTML。

I also have a file /app/views/folder1/show.html.erb that uses the tabs: 我还有一个使用选项卡的文件/app/views/folder1/show.html.erb

<h3>Tabs</h3><br>
<ul class="nav nav-tabs">
    <li class="active"><a href="#tab1" data-toggle="tab">Tab1</a></li>
    <li><a href="#tab2" data-toggle="tab">Tab2</a></li>
</ul>     
<div class="tab-content">
    <div class="tab-pane active" id="tab1">
        <div class="well">
        Text1
        </br>
        <%= @myobject.name %>
        </div>
    </div>
    <div class="tab-pane" id="tab2">
        <div class="well">
        Text2
        </div>
    </div>
</div>

However I need it to know where the javascript file is. 但是我需要它知道javascript文件在哪里。 What is the ruby way to do this? 红宝石的方法是什么? Would I still just put something like 我还会放一些像

<script src="/assets/javascriptss/bootstrap-tab.js"></script>

into the show.html.erb ? 进入show.html.erb吗? And if so, what path would I put? 如果是这样,我会走什么路?

There is no need for you to link to the Javascript file. 无需链接到Javascript文件。 Rails does this automatically and includes it in the Assets pipeline, which then makes it available to all your app. Rails会自动执行此操作,并将其包含在Assets管道中,然后使它可用于所有应用程序。 Read this guide: http://guides.rubyonrails.org/asset_pipeline.html . 阅读本指南: http : //guides.rubyonrails.org/asset_pipeline.html It discusses how to the asset pipeline works and its advantages. 它讨论了资产管道的工作方式及其优势。

If for some reason (I can't think of any), the javascript file is not being included in the asset pipeline even though it is in the appropriate location (as it seems to be), maybe you should explicitly include it in your application.js file. 如果出于某种原因(我想不出任何原因),即使javascript文件位于适当的位置(看上去也是如此)也没有包含在资产管道中,则也许您应该将其明确包含在应用程序中.js文件。

If you want the js file to be added to only 1 view and not other then you could use something like this 如果您想将js文件仅添加到1个视图中,而不要添加到其他视图中,则可以使用如下所示的内容

<%= javascript_include_tag "bootstrap-tab" %>

For more read 3.1.2 http://guides.rubyonrails.org/layouts_and_rendering.html#asset-tag-helpers 有关更多信息,请阅读3.1.2 http://guides.rubyonrails.org/layouts_and_rendering.html#asset-tag-helpers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM