我正在编写一些Ruby代码,而不是Rails,我需要处理这样的事情:
found 1 match
found 2 matches
Run Code Online (Sandbox Code Playgroud)
我安装了Rails所以也许我可以require在脚本的顶部添加一个子句,但有没有人知道一个多元化字符串的RUBY方法?如果脚本不是Rails但我安装了Rails,是否有一个我可以要求的类可以解决这个问题?
编辑:所有这些答案都很接近,但我检查了让它为我工作的那个.在编写Ruby而不是Rails代码时尝试使用此方法作为帮助:
def pluralize(number, text)
return text.pluralize if number != 1
text
end
Run Code Online (Sandbox Code Playgroud) 我刚刚安装了Paperclip的插件,我收到以下错误消息,但我不确定原因:
NoMethodError (undefined method `has_attached_file' for #<Class:0x10338acd0>):
/Users/bgadoci/.gem/ruby/1.8/gems/will_paginate-2.3.12/lib/will_paginate/finder.rb:170:in `method_missing'
app/models/post.rb:2
app/controllers/posts_controller.rb:50:in `show'
Run Code Online (Sandbox Code Playgroud)
它引用了will_paginate gem.从我能找到的,似乎我PostsController#index或者以前尝试安装gem而不是插件有问题,在这种情况下我读过我应该能够以/config/environments.rb某种方式通过文件来修复.
我之前认为以前的gem安装并不重要,因为我在安装插件之前删除了旧版本的网站.在该站点的当前版本中,我显示该表已在迁移后使用Paperclip列进行更新.这是我的代码:
PostsConroller#show:
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
Run Code Online (Sandbox Code Playgroud)
Post 模型:
class Post < ActiveRecord::Base
has_attached_file :photo
validates_presence_of :body, :title
has_many :comments, :dependent => :destroy
has_many :tags, :dependent => :destroy
has_many :votes, :dependent => :destroy
belongs_to :user
after_create :self_vote
def self_vote
# I am assuming you have a …Run Code Online (Sandbox Code Playgroud) 什么时候在Rails模型中使用attr_reader/ attr_writer/ attr_accessor?
我正在将我的开发环境从sqlite3切换到postgresql 8.4并且有最后一个障碍.
在我的原始中,我在辅助方法中有以下行;
result = Users.find(:all, :order => "name collate NOCASE")
Run Code Online (Sandbox Code Playgroud)
这提供了一个非常好的不区分大小写的搜索.我不能为postgresql复制这个.应该很容易 - 任何想法?
谢谢.
我似乎找不到快速切换PHP版本的方法.有没有相当于PHP的ruby版本管理器的东西?我需要在OS X上切换5.3和5.2.
我从sudo bundle install命令获得以下输出:
Fetching source index for `http://rubygems.org/`
Could not reach rubygems repository `http://rubygems.org/`
Could not find gem 'rspec-rails (>= 2.0.0.beta.22, runtime)' in any of the gem sources.
Run Code Online (Sandbox Code Playgroud)
我正确设置了$ http_proxy并且我已经添加了gem:--http-proxy =我的代理到〜/ .gemrc.这些设置允许我的gem命令工作,我希望他们会转换为bundler,但没有这样的运气.
思考sudo可能不会继承我的所有环境,我也将这些设置添加到我的root用户,但是nada.
此时,bundler阻止我部署我的应用程序,我发现很少有其他人遇到这个问题.如果没有人有答案,我将被迫从我的Rails应用程序(我不介意做...)中破解捆绑器
我已经安装了rvm,以确保我的本地开发版本的ruby与我的服务器相同,我正在使用的特定应用程序(ruby 1.8.7).我已经完成了这个,并安装了ruby 1.8.7确定.但是,当我尝试启动rails控制台时,我收到此错误:
Readline was unable to be required, if you need completion or history install readline then reinstall the ruby.
You may follow 'rvm notes' for dependencies and/or read the docs page http://rvm.beginrescueend.com/packages/readline/ . Be sure you 'rvm remove X ; rvm install X' to re-compile your ruby with readline support after obtaining the readline libraries.
Couldn't load Wirble: no such file to load -- wirble
Run Code Online (Sandbox Code Playgroud)
我已经阅读了错误引用的页面上的注释(http://rvm.beginrescueend.com/packages/readline/),并按照说明进行操作,其中涉及基本安装readline,卸载ruby 1.8.7,然后安装ruby 1.8.7再次获得readline支持.(实际上该页面使用ruby 1.9.2作为示例,但我假设它也应该与1.8.7一起工作.也许情况并非如此).
但是,我仍然得到同样的错误.有没有其他人经历过这个并想出来了?
感谢任何建议 - 最多
编辑 - 我正在使用Ubuntu 9.10顺便说一下相关的情况.
编辑 …
使用IRB,为什么Date&Time类会自动加载,但DateTime不是?我必须这样require 'date',这对我没有意义,因为我认为Date和DateTime都使用标准库'date'?
ruby-1.9.2-p290 :001 > Date
=> Date
ruby-1.9.2-p290 :002 > Time
=> Time
ruby-1.9.2-p290 :003 > DateTime
NameError: uninitialized constant Object::DateTime
from (irb):3
from /Users/kamilski81/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
ruby-1.9.2-p290 :004 > require 'date'
=> true
ruby-1.9.2-p290 :005 > require 'date'
=> false
ruby-1.9.2-p290 :006 > DateTime
=> DateTime
Run Code Online (Sandbox Code Playgroud) 我使用Sublime Text 2,但当我的代码要求用户输入时,它不会让我输入任何值.(换句话说:input()在Python和getsRuby中无法正确提示我输入).
Python抛出一个EOFError错误(当用户没有输入任何东西时你会得到什么).
Simply USER INPUT不适用于我的Sublime Text 2.
编辑:例如
try:
text = input('Enter something: ')
print text
except EOFError:
print('\nEOFError')
=> Enter something: #cannot enter anything to var "text"
=> EOFError
=> [Finished]
Run Code Online (Sandbox Code Playgroud) 我已经成功地使用了capistrano一段时间,并且在每个项目中我突然失去了部署能力.
环境:
我没有使用rsa_keys或任何我想要capistrano来提示用户和密码.突然它决定不要求密码,但确实要求用户.然后它回滚并给我以下错误.
[deploy:update_code] exception while rolling back: Capistrano::ConnectionError, connection failed for: sub.example.com (Net::SSH::AuthenticationFailed: Authentication failed for user user@sub.example.com)
connection failed for: sub.example.com (Net::SSH::AuthenticationFailed: Authentication failed for user user@sub.example.com)
Run Code Online (Sandbox Code Playgroud)
这发生在我的个人笔记本电脑和我的iMac上.它部署到两个不同的服务器(两个Linux)时发生
我在这里完全不知所措.有任何想法吗?
ruby ×10
rvm ×2
bundler ×1
capistrano ×1
deployment ×1
homebrew ×1
paperclip ×1
php ×1
pluralize ×1
postgresql ×1
proxy ×1
python ×1
rbenv ×1
readline ×1
require ×1
rubygems ×1
sql-order-by ×1
sublimetext2 ×1