如何将 jquery-ui 添加到 Ruby on Rails 3.1 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6237396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:19:15  来源:igfitidea点击:

How do I add jquery-ui to a Ruby on Rails 3.1 app?

ruby-on-railsjquery-ui

提问by chris

I have added //=require jquery-uito my application.js file and that seems to give me the javascript but I can't seem to get the stylesheets to be included. If I understand it right they should go in vendor/stylesheetsbut how do I get them to be included?

我已经添加//=require jquery-ui到我的 application.js 文件中,这似乎给了我 javascript,但我似乎无法包含样式表。如果我理解正确,他们应该进入,vendor/stylesheets但我如何让他们被包括在内?

采纳答案by chris

denysonique has the answer in this question as pointed out by epochwolf in the comments.

正如 epochwolf 在评论中指出的那样,denysonique 在这个问题上有答案。

Rails 3.1 and jquery-ui assets

Rails 3.1 和 jquery-ui 资产

回答by Jo Liss

Perhaps it's easier to use the jquery-ui-rails gem(see announcement). It packages everything up so things "just work".

也许使用jquery-ui-rails gem更容易(请参阅公告)。它把所有东西都打包起来,所以事情“正常工作”。

回答by daniel

You can use google CDN, to add the css theme inside the head section of your app. Simply add this application.html.haml under the %head section(or the same thing translated to erb).

您可以使用 google CDN,在应用的 head 部分中添加 css 主题。只需将此 application.html.haml 添加到 %head 部分下(或翻译为 erb 的相同内容)。

The css theme

css 主题

%link{:href => "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/themes/ui-lightness/jquery-ui.css", :rel => "stylesheet", :type => "text/css"}

If you want the jquery-ui minified.

如果你想缩小 jquery-ui。

%script{:src => "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/jquery-ui.min.js"}

回答by Aldo

if you include 'jquery-rails' in Gemfile jquery-ui will be included requiring this in application.js:

如果你在 Gemfile 中包含 'jquery-rails',jquery-ui 将被包含在 application.js 中,要求这个:

//= require jquery-ui.min

//= 需要 jquery-ui.min

if you run in Console: Rails.application.config.assets.pathsyou will get all paths Rails will look into for assets. In my case for instance:

如果您在 Console 中运行: Rails.application.config.assets.paths您将获得 Rails 查找资产的所有路径。以我为例:

- /Users/aldo/Satio/Desarrollo/rails/subaquaclub/app/assets/images
- /Users/aldo/Satio/Desarrollo/rails/subaquaclub/app/assets/javascripts
- /Users/aldo/Satio/Desarrollo/rails/subaquaclub/app/assets/stylesheets
- /Users/aldo/Satio/Desarrollo/rails/subaquaclub/vendor/assets/javascripts
- /Users/aldo/Satio/Desarrollo/rails/subaquaclub/vendor/assets/stylesheets
- /Users/aldo/.rvm/gems/ruby-1.9.2-p290@subaquaclub31/gems/jquery-rails-1.0.13/vendor/assets/javascripts

See the last line ? if you check in there you will find jquery-ui so there you go.

看到最后一行了吗?如果你在那里签到,你会发现 jquery-ui,所以你去。

回答by Markus

quote from the jquery-rails manual:

引用自jquery-rails 手册

In order to use the themed parts of jQuery UI, you will also need to supply your own theme CSS. See jqueryui.com for more information.

为了使用 jQuery UI 的主题部分,您还需要提供自己的主题 CSS。有关更多信息,请参阅 jqueryui.com。

So, you have to include or style them yourself! Just include the theme's .cssin the header of your page. In Rails 3 you'd put the css in public/stylesheets, don't know for Rails 3.1.

因此,您必须自己包含或设置它们的样式!只需.css在页面的标题中包含主题。在 Rails 3 中,您将 css 放入public/stylesheets,不知道 Rails 3.1。

回答by Michael Yagudaev

jquery-rails no longer has jquery-ui as part of its assets. You should use gem 'jquery-ui-rails'for that.

jquery-rails 不再将 jquery-ui 作为其资产的一部分。你应该使用gem 'jquery-ui-rails'它。

Furthermore, to find out where an asset is coming form in rails you can do the following:

此外,要在 rails 中找出资产的形成位置,您可以执行以下操作:

paths = Rails.application.config.assets.paths
for path in paths do
  puts "Found in: #{path}" if Dir.glob("#{path}/*").grep(/jquery-ui/).present?
end

This should make it easy to find the asset.

这应该可以很容易地找到资产。

回答by Sachin Prasad

Just remember //= require jquery.ui.allneeds to be after //= require_tree .

只记得//= require jquery.ui.all需要之后//= require_tree .

Wasted a lot of my time because of this as most of Jquery UI functionalities won't work.

由于大多数 Jquery UI 功能无法正常工作,因此浪费了我很多时间。

回答by Gabriel Mazetto

If you have jquery-rails on Gemfile, just do:

如果您在 Gemfile 上有 jquery-rails,请执行以下操作:

//= require jquery-ui

or

或者

//= require jquery-ui.min

both will work fine. If not, try to update jquery-rails gem

两者都可以正常工作。如果没有,请尝试更新 jquery-rails gem

Don't forget the css files, here I followed some articles on that other answer: Ruby on Rails 3.1 and jQuery UI imagesand got it working by this way:

不要忘记 css 文件,在这里我关注了有关其他答案的一些文章:Ruby on Rails 3.1 和 jQuery UI 图像并通过这种方式使其工作:

*= require jquery-ui/jquery-ui.css

this is the path for files:

这是文件的路径:

vendor/assets/stylesheets/jquery-ui/jquery-ui.css 
vendor/assets/images/jquery-ui/images/ (your theme images)