由于最新的Rails 3版本不再自动加载来自lib的模块和类,加载它们的最佳方法是什么?
来自github:
Run Code Online (Sandbox Code Playgroud)A few changes were done in this commit: Do not autoload code in *lib* for applications (now you need to explicitly require them). This makes an application behave closer to an engine (code in lib is still autoloaded for plugins);
这是排序哈希并返回哈希对象(而不是数组)的最佳方法:
h = {"a"=>1, "c"=>3, "b"=>2, "d"=>4}
# => {"a"=>1, "c"=>3, "b"=>2, "d"=>4}
Hash[h.sort]
# => {"a"=>1, "b"=>2, "c"=>3, "d"=>4}
Run Code Online (Sandbox Code Playgroud) 我的应用程序中有一个典型的搜索工具,它返回一个结果列表,可以对其进行分页,排序,使用不同的records_per_page值进行查看等.每个选项都由查询字符串中的参数控制.一个简化的例子:
/search?q=test&page=2
Run Code Online (Sandbox Code Playgroud)
现在说我需要显示一组将records_per_page值设置为10,20,30的链接.每个链接必须包含现有的查询参数,这些参数可以是一个非常长的集合,加上一个新的per_page参数.
/search?q=test&page=2& ... &per_page=10
/search?q=test&page=2& ... &per_page=20
/search?q=test&page=2& ... &per_page=30
Run Code Online (Sandbox Code Playgroud)
是否有一个简单的方法只使用link_to帮助器或我需要以某种方式解析并重现先前请求的查询字符串?
是否有一种防弹方法来检测Ruby或Ruby on Rails中上传文件的MIME类型?我使用的SWFUpload上传的JPEG和PNG图像,并content_type始终"application/octet-stream"
My Rails 3应用程序以纯文本和HTML格式发送电子邮件.我使用RoundCube和Squirrel Mail客户端在本地测试了它们,它们都显示带有图像,链接等的HTML版本.另一方面,GMail选择纯文本格式.知道是什么导致了这个吗?
Delivered-To: test@gmail.com
Received: by 10.42.166.2 with SMTP id m2cs16081icy;
Thu, 3 Mar 2011 17:01:48 -0800 (PST)
Received: by 10.229.211.138 with SMTP id go10mr1544841qcb.195.1299200507499;
Thu, 03 Mar 2011 17:01:47 -0800 (PST)
Return-Path: <info@example.com>
Received: from beta.example.com (testtest.test.com [69.123.123.123])
by mx.google.com with ESMTP id j14si1690118qcu.136.2011.03.03.17.01.46;
Thu, 03 Mar 2011 17:01:46 -0800 (PST)
Received-SPF: neutral (google.com: 69.123.123.123 is neither permitted nor denied by best guess record for domain of info@example.com) client-ip=69.123.123.123;
Authentication-Results: mx.google.com; spf=neutral (google.com: 69.123.123.123 is neither permitted …Run Code Online (Sandbox Code Playgroud) Process.fork与Ruby 1.9.2中的新Process.spawn方法有什么区别,哪一个更好地在子进程中运行另一个程序?据我所知,Process.fork接受代码块,Process.spawn接受系统命令加上一些其他参数.当我应该使用一个而不是另一个?
任何人都可以为Rails 3推荐一个简单,轻量级的CMS gem或插件,可以轻松嵌入到现有的应用程序中吗?
为什么以下不起作用:
//iframe:
window.parent.$(document).trigger('complete');
//parent window:
$(document).bind('complete', function(){
alert('Complete');
});
Run Code Online (Sandbox Code Playgroud)
而以下IS工作:
//iframe:
window.parent.$('body').trigger('complete');
//parent window:
$('body').bind('complete', function(){
alert('Complete');
});
Run Code Online (Sandbox Code Playgroud)
?
获取控制器中公用文件夹内文件的绝对路径的最佳方法是什么?是否有预定义的Rails变量保存公用文件夹的绝对路径?
目前我正在使用File.expand_path('../../../public', __FILE__),但我确信有更好的方法可以做到这一点.