Ruby是否有任何Formatter类或方法可用于格式化货币等数字的数字,或者是否有任何宝石执行此操作,或者您是否必须自己编写?
我有一个控制器叫ProjectsController.默认情况下,它的操作会查找内部视图app/views/projects.我想改变这种路径的所有方法(index,show,new,edit等...)在控制器中.
例如:
class ProjectsController < ApplicationController
#I'd like to be able to do something like this
views_path 'views/mycustomfolder'
def index
#some code
end
def show
#some code
end
def new
#some code
end
def edit
#some code
end
end
Run Code Online (Sandbox Code Playgroud)
请注意我并没有更改每个方法,render而是为所有方法定义默认路径.这可能吗?如果是这样,怎么样?
谢谢!
我想知道是否有任何相对简单的方法来加快我的控制台加载时间,这开始接近30秒.我有很多子类,其方法似乎没有受到影响reload!所以我最终打开和关闭控制台很多.IRB快速加载闪电.
我有太多宝石吗?我如何计算负载任务的时间,以便我可以看到占用时间最多的是什么?正如你所看到的,我已经尝试过dev-boost gem无济于事.该应用程序在Passenger中很好,它只是控制台加载,从而导致我的废话.运行在具有2.4GHz和4GB RAM的MBP OSX 10.6.6上.不使用RVM.
版本:
Ovid$ rails -v
Rails 3.0.3
Ovid$ ruby -v
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10]
Run Code Online (Sandbox Code Playgroud)
记忆:
Ovid$ vm_stat
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 118818.
Pages active: 341320.
Pages inactive: 99490.
Pages speculative: 310576.
Pages wired down: 112527.
"Translation faults": 23097323.
Pages copy-on-write: 1270961.
Pages zero filled: 13836659.
Pages reactivated: 36.
Pageins: 165761.
Pageouts: 0.
Object cache: 28 hits of 760846 lookups (0% hit rate)
Run Code Online (Sandbox Code Playgroud)
的Gemfile:
source …Run Code Online (Sandbox Code Playgroud) 我怎样才能做到这一点?
试图创建2个方法,称为
def disable_timestamps
ActiveRecord::Base.record_timestamps = false
end
def enable_timestamps
ActiveRecord::Base.record_timestamps = true
end
Run Code Online (Sandbox Code Playgroud)
和更新方法本身:
def increment_pagehit
update_attribute(:pagehit, pagehit+1)
end
Run Code Online (Sandbox Code Playgroud)
使用回调来打开和关闭时间戳,例如:
before_update :disable_timestamps, :only => :increment_pagehit
after_update :enable_timestamps, :only => :increment_pagehit
Run Code Online (Sandbox Code Playgroud)
但它没有更新任何东西,甚至是所需的属性(pagehit).
有什么建议?我不想创建另一个表来计算分页数.
说:
h = { 1 => 10, 2 => 20, 5 => 70, 8 => 90, 4 => 34 }
Run Code Online (Sandbox Code Playgroud)
我想将每个值更改v为foo(v),这h将是:
h = { 1 => foo(10), 2 => foo(20), 5 => foo(70), 8 => foo(90), 4 => foo(34) }
Run Code Online (Sandbox Code Playgroud)
实现这一目标的最优雅方式是什么?
例如,我bundler?在以下代码段中找到了方法名称,并且不知道该?字符是专用关键字还是仅仅是方法名称的一部分.
# This is a predicate useful for the doc:guides task of applications.
def bundler?
# Note that rake sets the cwd to the one that contains the Rakefile
# being executed.
File.exists?('Gemfile')
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Rails 4和Turbolinks在具体页面中创建JS代码段.我尝试过标准解决方案:
<script type="text/javascript">
var ready = function() {
// Bla bla
};
$(document).ready(ready);
$(document).on('page:load', ready);
</script>
Run Code Online (Sandbox Code Playgroud)
但它似乎没有用.我的代码片段是这样的:
<script type="text/javascript">
function ismready()
{
var API_KEY = "api key";
var roomId = "room id";
var ism = new Ismuser(API_KEY, roomId);
ism.init({
board: {
layer: "board"
},
video: {
camsContainer: "guest"
},
chat: {
layer: "chat"
},
moderator: true,
});
}
</script>
<script src="http://assets.ismuser.com/v0.4/js/ismuser.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
该片段无法按预期工作(即使使用标准解决方案),最后我只想在此页面中禁用Turbolinks.
我该怎么做?
- 解决方案
<% content_for :body do %>
<% if controller.controller_name == 'home' && controller.action_name == …Run Code Online (Sandbox Code Playgroud) 我的用户表登录列是String类型,限制为40个字符.现在我计划将限制增加到55个字符.
任何人请告诉我如何通过使用ROR迁移来增加此限制.
谢谢,Sravan
我一直在尝试将RVM和Ruby 2.1.1安装到我的新Mac(OS 10.9.2)上,在我尝试安装任何版本的Ruby之前,一切似乎都进展顺利.
我的意见
$ rvm reinstall 2.1
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好...
ruby-2.1.1 - #removing rubies/ruby-2.1.1..
Searching for binary rubies, this might take some time.
Found remote file https://rvm.io/binaries/osx/10.9/x86_64/ruby-2.1.1.tar.bz2
Checking requirements for osx.
Certificates in '/usr/local/etc/openssl/cert.pem' already are up to date.
Requirements installation successful.
ruby-2.1.1 - #configure
ruby-2.1.1 - #download
ruby-2.1.1 - #validate archive
ruby-2.1.1 - #extract
ruby-2.1.1 - #validate binary
ruby-2.1.1 - #setup
ruby-2.1.1 - #making binaries executable..
ruby-2.1.1 - #downloading rubygems-2.2.2
ruby-2.1.1 - #extracting rubygems-2.2.2....
ruby-2.1.1 - #removing old rubygems.........
Run Code Online (Sandbox Code Playgroud)
但是之后... …
为什么这在Ruby中有效:
"foo" "bar"
# => "foobar"
Run Code Online (Sandbox Code Playgroud)
我不确定为什么字符串被连接而不是给出语法错误.
我很好奇这是否是预期的行为,以及它是否是解析器负责争吵的东西(没有运算符的两个字符串被认为是单个字符串)或者语言定义本身指定了这种行为(隐式concat).