简体   繁体   English

如何在Rails 3中使用jQuery Uniform

[英]How to use jQuery Uniform with Rails 3

I'm trying to get the Default Uniform up on my rails webapp, but have not yet found any solid tutorial/guide. 我正在尝试在我的rails webapp上安装“默认制服”,但尚未找到任何可靠的教程/指南。 I've confirmed that I have the jquery gem 我已经确认我有jquery gem

In my Gemfile: 在我的Gemfile中:

gem 'jquery-rails', '2.0.2'

and then ran in the terminal from within my app directory to make sure: 然后从我的应用程序目录中运行终端,以确保:

bundle install

I put uniform.default.css in my stylesheets directory, jquery.uniform.js in my javascripts directory, and the images in my images directory. 我在我的样式表目录中放置了Uniform.default.css,在javascripts目录中放置了jquery.uniform.js,在图像目录中放置了图像。 I then include them in my view with the form: 然后,我将其包括在表单中:

  1 <!-- for jQuery uniform -->
  2 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
  3 <%= javascript_include_tag "jquery.uniform.js" %>
  4 <%= stylesheet_link_tag "uniform.default.css" %>
  5 <script type='text/javascript'>
  6   // On load, style typical form elements
  7   $(function () {
  8     $("select, input, button, textarea, submit").uniform();
  9   });
 10 </script>
 11 <!-- end for jQuery uniform -->

No, images are showing up though! 不,不过图像正在显示! I peeked into the source of uniform.default.css file and found that all the images are relative to the current directory: 我查看了uniform.default.css文件的源,发现所有图像都相对于当前目录:

  background-image: url("../images/sprite.png");

Luckily, that still seems to be the case the way the default tree of the rails 3 app is set up, but there are issues. 幸运的是,Rails 3应用程序的默认树的设置方法似乎仍然如此,但是存在一些问题。 I also tried editing every instance of this to be an absolute path that I've confirmed to be valid with another image. 我还尝试将其的每个实例编辑为一个绝对路径,该路径已确认对其他图像有效。 eg: 例如:

  background-image: url("/assets/images/sprite.png");

But still no luck. 但是仍然没有运气。 Any ideas? 有任何想法吗? Does the javascript also depend on a particular path for the images? JavaScript是否还取决于图像的特定路径? It shouldn't, right? 不应该吧?

Try adding the files in the vendor/assets directory. 尝试将文件添加到vendor / assets目录中。 in there put the css in the 放在里面的CSS

vendor/assets/stylesheets

the js in js中

vendor/assets/javascripts

and the images in 和中的图像

vendor/assets/images

and than in add the following line to app/assets/stylesheets/application.css: 然后将以下行添加到app / assets / stylesheets / application.css:

*= require uniform.agent

Rename the js file that you placed in vendor/assets/javascripts to uniform.js and add this in the app/assets/javascripts/application.js 将您放置在供应商/资产/ javascripts中的js文件重命名为uniform.js,并将其添加到app / assets / javascripts / application.js中

//= require uniform

Phew! Took some experimentation, but finally integrated jQuery Uniform like so: 进行了一些实验,但最终像这样集成了jQuery Uniform:

Ensure you have the jQuery gem installed by including this in your Gemfile: gem 'jquery-rails', '2.0.2' 通过在Gemfile中包含以下内容来确保已安装jQuery gem:gem'jquery gem 'jquery-rails', '2.0.2'

Download whatever style you're interested in from: http://uniformjs.com/#themes 从以下位置下载您感兴趣的任何样式: http//uniformjs.com/#themes

Add the images to /vendor/assets/images, the javascript file to vendor/assets/javascripts and the stylesheet to vendor/assets/stylesheets. 将图像添加到/ vendor / assets / images,将javascript文件添加到vendor / assets / javascripts,将样式表添加到vendor / assets / stylesheets。

Edit any background url image paths in the css file from: url('../images/imagename.png') to 将CSS文件中的任何背景url图像路径从url('../ images / imagename.png')编辑为

url('/assets/imagename.png')

So all these assets are eventually plopped into one big directory I guess, named assets. 因此,所有这些资产最终都会放入一个大目录,即资产。 The separation is only for the developer's sanity. 分开只是为了开发商的理智。

I also made sure to include that in the head of my main layout, application.html.erb : 我还确保将其包括在主布局的顶部application.html.erb

<%= stylesheet_link_tag "application", :media => "all" %>
<%= stylesheet_link_tag "uniform.aristo", :media => "all" %>  <!-- for jQuery Uniform -->
<%= javascript_include_tag "application" %>
<%= javascript_include_tag "jquery.uniform" %> <!-- for jQuery Uniform -->

That's all. 就这样。 No obscure editing of config files or random bug fixes that need other fixes that require more wizard code, ... 无需对配置文件进行模糊的编辑,也无需对其他需要更多向导代码的修复进行随机的错误修复,...

It's much more straightforward now that I got a version working and then stripped away any potential influence from all my other experiments. 现在,我可以使用一个版本,然后从所有其他实验中删除所有潜在影响,这变得更加直接。

Hope it helps. 希望能帮助到你。

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

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