jQuery Datepicker 不适用于 Heroku/Rails 3.1
大家好,
我对 jQuery Datepicker 和 Rails 3.1 感到非常沮丧。 它在我的开发环境中运行完美,但在 Heroku/Cedar 的生产环境中无法运行。当我选择日期字段时,它根本不会弹出。
这就是我到目前为止所做的:
我将 jquery 文件放入 \apps\assets\javascript 中:
- jquery-1.7.1.min.js
- jquery-ui-1.8.17.custom.min.js
- jquery.ui.datepicker-de。 我将
它们与其他文件一起添加到我的 production.rb 中:
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) <br />
config.assets.precompile += %w( event_calendar.js event_calendar.css jquery-1.7.1.min.js jquery-ui-1.8.17.custom.min.js jquery-ui-1.8.17.custom.css jquery.ui.datepicker-de.js)
然后我在本地预编译了我的资源如此处所述,并且它们正确显示在 /public/assets/< /strong> 和我的 manifest.yml 中。因此,一切看起来都不错,并且在将应用程序推送到 Heroku 时没有出现任何错误,但是当我选择与日期选择器关联的文本字段时,什么也没有发生,它根本不显示。
你知道这里可能出了什么问题吗?
我怎样才能找出问题所在?
在 Heroku 日志中,我只能找到这样的条目:
cache: [GET /assets/application-58ec8ec4b5384a8fb8d7884169385e49.css] miss
如果有人可以提供帮助,我会很高兴。
更新:
这是我的 application.js
:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .
$(document).ready(
function()
{
$("#picker").datepicker($.datepicker.regional['de']);
$("#picker").datepicker();
}
);
据我所知 //= require_tree .
负责将所有 javascript 文件包含在 asset 文件夹中,例如 \app \assets\javascripts\
。这是否包括 \public\assets
中的预编译资产???。
这是我的 application.css
:
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require_tree .
*/
...
同样,我认为 *= require_tree .
负责 \app\assets\stylesheets
作为以及 \public\assets
,我的预编译样式表所在的位置。
这里是 Heroku 上应用程序的链接,选择日期字段应该会弹出日历:
http://smooth-window-1858.herokuapp.com/events/new
Hello everybody out there,
I'm very frustrated with the jQuery Datepicker and Rails 3.1.
It runs perfectly in my develoment environment, but it doesn't work in the production environment on Heroku/Cedar. It simply doesn't pop-up when I select my date field.
So here's what I did so far:
I put the jquery files into \apps\assets\javascript:
- jquery-1.7.1.min.js
- jquery-ui-1.8.17.custom.min.js
- jquery.ui.datepicker-de.js
and I added them among other files to my production.rb:
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) <br />
config.assets.precompile += %w( event_calendar.js event_calendar.css jquery-1.7.1.min.js jquery-ui-1.8.17.custom.min.js jquery-ui-1.8.17.custom.css jquery.ui.datepicker-de.js)
Then I precompiled my assets locally as described here and they appeared correctly in /public/assets/ and in my manifest.yml. So everything looks good and I don't get any errors while pushing my app to Heroku, but when I select my text field associated with the datepicker nothing happens, it simply doesn't show up.
Any ideas what could be wrong here?
How can I find out what the problem is?
In the Heroku Logs I can only find entries like this:
cache: [GET /assets/application-58ec8ec4b5384a8fb8d7884169385e49.css] miss
Would be happy if somebody could help, please.
Update:
Here's my application.js
:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .
$(document).ready(
function()
{
$("#picker").datepicker($.datepicker.regional['de']);
$("#picker").datepicker();
}
);
As far as I know //= require_tree .
takes care of including all javascript files in the assets folder, like \app\assets\javascripts\
. Does that include precompiled assets in \public\assets
???.
And here's my application.css
:
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require_tree .
*/
...
Same here, I thought *= require_tree .
takes care of \app\assets\stylesheets
as well as for \public\assets
, where my precompiled stylesheets are.
Here the link to the app on heroku, selecting the date field should pop-up the calendar:
http://smooth-window-1858.herokuapp.com/events/new
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与此同时,我发现我的项目出了什么问题。
我必须在
\config\environments\production.rb
中禁用 Rails 的静态资源服务器:此外,我必须将
jquery-rails
添加到我的 Gemfile 中:现在日期选择器工作正常,我从
/app/assets/javascript
中删除了 jquery .js 文件,因为 gem 会处理这个问题。不过,我认为jquery文件是在资源预编译过程中添加的。他们从哪里来?当我在布局中简单地引用 Google CDN 时,资产预编译(Heroku 要求)如何工作,例如:
In the meantime I found out what's wrong with my project.
I had to disable Rails's static asset server in
\config\environments\production.rb
:Overmore, I had to add
jquery-rails
to my Gemfile:Now the datepicker works fine, and I removed the jquery .js files from
/app/assets/javascript
, because the gem takes care of that. However, I think jquery files are added in the process of asset precompilation. Where do they come from?And how can asset precompilation (required by Heroku) work, when I simply refer the Google CDN in my layout, like:
我猜你缺少 css 文件,而不是 js 文件。检查错误。确保 stylesheets 文件夹中存在
jquery-ui-1.8.17.custom.css
,并且 stylesheets 文件夹中应该有一个 datepicker 样式表,以便 datepicker 应该显示出来。如果错误仍然存在,则将您的
application.js
文件和application.css
文件粘贴到此处。You are missing css file, not js file i guess. Check the error. Make sure
jquery-ui-1.8.17.custom.css
exists in stylesheets folder, also there should be a datepicker stylesheet in your stylesheets folder so that datepicker should shows off.If the error is still there then Paste your
application.js
file andapplication.css
file here.它们可能没有显示的原因是您没有在视图中正确引用它们。
将文件添加到预编译确实可以确保有一个文件副本可用于辅助方法中的直接引用,但它不会在视图中包含这些文件。
如果您确实在视图中引用它们,则需要使用 Rails 辅助方法而不是硬链接到它们。 Rails 的助手负责插入正确的生产文件名。
正常方法(最佳实践)是在 application.js 和 application.css 清单中需要这些文件。
您不应在生产日志中看到缓存未命中/命中条目。这表明您的应用程序没有使用预编译文件,而是将请求传递给 Sprockets。
出现这种情况有两个可能的原因:一是您硬链接到文件(即未使用辅助方法),二是管道配置不正确。
根据资产最后一部分中的配置检查您的配置设置管道指南。
回复更新和评论:
require_tree 将 javascript 和 stylsheets 文件夹中的文件导入到主清单中。
对于 JS,您将按顺序在 application.js 中拥有所有必需的文件。这些必需的文件都不存在于公共/资产中,因为它们被卷入清单中并且不必单独预编译(它们可以从预编译数组中删除)。
您只需要具体说明文件顺序很重要的地方 - 例如 jquery 和 jquery ujs 应该按照您拥有它们的顺序排在第一位。
The reason they probably don't show is that you have not referenced them correctly in you views.
Adding files to precompile does ensures that there is a copy of the files available for direct reference in a helper method, but it does not include those files in views.
If you do reference them in your views, you need to use the Rails helper methods and not hard link to them. The Rails' helpers handle inserting the correct production filenames.
The normal way (the best practice) is to require these files inside your application.js and application.css manifests.
You should not see cache miss/hit entries in your production log. This is an indication that your application is not using the precompiled files, but passing the requests to Sprockets.
There are two possible reasons for this: one is that you have hard linked to the files (i.e. not used the helper methods) and two, the pipeline is not configured correctly.
Check your configuration settings against those in the last section of the asset pipeline guide.
Reply to Update and comment:
require_tree IMPORTS the files in the javascript and stylsheets folder into the master manifests.
In the case of your JS you would have all those required files in your application.js in that order. None of those required files would exist in public/assets because they are rolled into the manifest and do not have to be separately precompiled (they can be removed from the precompile array).
You only have to be specific where the order of files is important - for example jquery and jquery ujs should come first in the order you have them.