Tag: ruby

如何处理Ruby on Rails错误:“请安装postgresql适配器:`gem install activerecord-postgresql-adapter'”

运行使用ActiveRecord框架的Ruby on Rails(RoR)应用程序或Ruby代码时,您会收到错误消息: 请安装postgresql适配器: gem install activerecord-postgresql-adapter 试图运行: gem install activerecord-postgresql-adapter 也失败了,让你无所适从。

Ruby的双冒号(::)运算符用法差异

有什么区别吗? module Foo class Engine < Rails::Engine end end 和 module Foo class Engine < ::Rails::Engine end end

Ruby运算符优先级表

给我一个明确的,同行评审/维护的Ruby优先级表( 运算符 , 非运算符和修饰符 )。 多年来,我不得不依靠以下来源获取这些信息: 1. http://phrogz.net/programmingruby/language.html#table_18.4 – Pickaxe书,它logging了2000年9月发布的Ruby 1.6 ,包括格式错误或错字( {被列为作业运营商)。 2. http://www.techotopia.com/index.php/Ruby_Operator_Precedence-上面的Pickaxe表的近似副本,包括错误的{ ,并且意外地描述|| 作为逻辑“AND” 。 3. http://www.tutorialspoint.com/ruby/ruby_operators.htm – 也是Pickaxe表的一个近似副本,虽然它修复了对||的描述。 逻辑“或” ,但它仍然列出{作为一个赋值运算符。 而且,它列出::并将其错误地描述为一个常量parsing运算符 ( :: 不是运算符)。 4. http://my.safaribooksonline.com/book/web-development/ruby/9780596516178/expressions-and-operators/operators – Ruby编程语言手册,它logging了Ruby 1.8和1.9 ,它们于2003年8月发布, 2007年12月,分别。 这本书于2008年由David Flanagan和Yukihiro Matsumoto(Ruby的发明者,“Matz”)发表。 它似乎是运营商,非运营商,修改者和支持信息的最新和准确的清单。 顺便说一句,大约在2005年,对Ruby的兴趣与Rails(2004年7月发布)一起激增。 5. http://romhack.wikia.com/wiki/Ruby_operators – 还在Ruby 1.9文档,并在其表中包含非运算符和修饰符。 Ruby 2.0于2013年2月发布,旨在完全向后兼容Ruby 1.9.3 。 less数已知的不兼容性,与运营商无关。 Ruby 2.1.0于2013年圣诞节当天发布 ,同样没有列出运营商的不兼容性。 因此,我决定在Flanagan / […]

铁轨3 – link_to销毁不工作

我试图创build一个销毁链接到我的用户控制器,我也使用devise。 这是我的代码 – 视图 <%= link_to 'Delete User?', child, :confirm => "Are you sure you want to delete #{child.full_name}?", :method => :delete, :class => "user-additional", :style => "font-size:12px;font-weight:normal;" %> 调节器 def destroy if @user = User.find(params[:id]) @user.destroy respond_to do |format| format.html { redirect_to account_index_path } format.xml { head :ok } end end end 路线 devise_for […]

ActiveRecord Arel OR条件

你怎么能结合2个不同的条件使用逻辑OR而不是AND? 注意: 2个条件是作为rails范围生成的,不能直接更改为where("x or y") 。 简单的例子: admins = User.where(:kind => :admin) authors = User.where(:kind => :author) 应用AND条件很容易(对于这个特殊的情况是毫无意义的): (admins.merge authors).to_sql #=> select … from … where kind = 'admin' AND kind = 'author' 但是,怎样才能生成以下具有2种不同的Arel关系的查询呢? #=> select … from … where kind = 'admin' OR kind = 'author' 看来( 根据Arel自述 ): OR运算符尚不支持 但我希望它不适用于此,并希望写下如下内容: (admins.or authors).to_sql

如何获得一个单一的字符,而无需按下input?

怎样才能从terminal用Rubyinput一个单一的键盘字符? 我尝试了Curses::getch ,但那对我来说并不是真的。

Ruby风格:如何检查是否存在嵌套的哈希元素

考虑存储在散列中的“人”。 两个例子是: fred = {:person => {:name => "Fred", :spouse => "Wilma", :children => {:child => {:name => "Pebbles"}}}} slate = {:person => {:name => "Mr. Slate", :spouse => "Mrs. Slate"}} 如果“人”没有孩子,则“孩子”元素不存在。 那么,对于斯莱特先生,我们可以检查他是否有父母: slate_has_children = !slate[:person][:children].nil? 那么,如果我们不知道“石板”是一个“人”哈希呢? 考虑: dino = {:pet => {:name => "Dino"}} 我们再也不能轻易地检查孩子了: dino_has_children = !dino[:person][:children].nil? NoMethodError: undefined method `[]' for nil:NilClass […]

Rails has_manydynamic条件

我想要的是创build一个模型,以dynamic的方式使用has_many关联与另一个连接,不需要像这样的外键: has_many :faixas_aliquotas, :class_name => 'Fiscal::FaixaAliquota', :conditions => ["regra_fiscal = ?", ( lambda { return self.regra_fiscal } ) ] 但是我得到的错误: : SELECT * FROM "fis_faixa_aliquota" WHERE ("fis_faixa_aliquota".situacao_fiscal_id = 1 AND (regra_fiscal = E'— !ruby/object:Proc {}')) 这可能吗?

添加自定义字段/列来deviseRails 4

我试图添加一个full_name字段/列到我的用户模型(使用devisegem)和Rails 4。 大多数的在线示例都推荐使用attr_accessible ,但是听起来Rails 4中应该采用不同的方式。 我如何将full_name添加到我的用户模型? 我已经能够成功运行迁移。 文件:迁移> add_full_name_to_users class AddFullNameToUsers < ActiveRecord::Migration def change add_column :users, :full_name, :string end end 文件:注册> app / views / devise / registration / new.html . . . <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> <%= devise_error_messages! %> <%= f.label :full_name %> <%= f.text_field :full_name, […]

在Ruby模块中执行每个方法调用的代码

我在Ruby 1.9.2中编写了一个定义了几个方法的模块。 当任何这些方法被调用,我希望他们每个人先执行一个特定的陈述。 module MyModule def go_forth a re-used statement # code particular to this method follows … end def and_multiply a re-used statement # then something completely different … end end 但是我想避免在每一个方法中明确地a re-used statement代码。 有没有办法做到这一点? (如果重要的话, a re-used statement会在每个方法被调用时打印自己的名字,这将通过puts __method__一些变体来puts __method__ 。