Tag: ruby 2.2

Rails和RSpec:在不同的命名空间(模块)中测试具有相同名称的控制器

我有使用RSpec 3.4.0测试的rails 4.1.16 API应用程序,我遇到了在不同模块中测试名为同名的类的问题。 结构是: app/controllers/bar/notifications_controller.rb class Bar::NotificationsController < ApiController … end 和控制器在不同的模块中具有相同的名称: app/controllers/foo/bar/notifications_controller.rb module Foo class Bar::NotificationsController < ApiController … end end Foo是一个新模块,还没有测试。 添加之后,旧的Bar::NotificationsController所有相应控制器测试都开始失败。 规范文件: spec/controllers/bar/notifications_controller_spec.rb require ‘spec_helper’ describe Bar::NotificationsController, type: :controller do … end 该规范文件中的所有测试都失败并出现相同的错误: RuntimeError: @controller is nil: make sure you set it in your test’s setup method. 当我更改Foo模块中的控制器名称时,问题不存在: app/controllers/foo/bar/foo_notifications_controller.rb module Foo […]

资产管道在我的Rails 4.2应用程序中的’application.scss’中不起作用

我是Rails的新手并且如果这是一个愚蠢的问题而道歉。 但我无法解决我的问题:我添加了Bootstrap到我的新应用程序,但它仍然没有使用任何新的样式。 我已将application.css重命名为application.scss并创建了这样的结构: |-stylesheets |-elements |–articles |—article.scss |-application.scss Application.scss /* User styles * Plugins first */ @import “bootstrap-sprockets”; @import “bootstrap”; @import “elements/**/*”; /* * This is a manifest file that’ll be compiled into application.css, which will include all the files * listed below. * * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, […]

为什么Ruby中有时需要括号?

我最近在查看Rails文档中的一些Ruby代码时遇到了一个奇怪的问题。 Ruby允许您传递像这些示例的参数: redirect_to post_url(@post), alert: “Watch it, mister!” redirect_to({ action: ‘atom’ }, alert: “Something serious happened”) 但那第二个案子对我来说很奇怪。 看起来你应该能够像这样传递它: redirect_to { action: ‘atom’ }, alert: “Something serious happened” 无论有没有括号,它都有相同的含义。 但相反,你得到: syntax error, unexpected ‘:’, expecting ‘}’ action后提到结肠。 我不确定它为什么会在那里,以及为什么使用括号会改变它。

为什么“instance.send(:initialize,* args,** kwargs,&block)”只能从Class#new中失败?

我已经坚持了很长一段时间了。 看看这个: class SuperClass def self.new(*args, **kwargs, &block) i = allocate() # Extra instance setup code here i.send(:initialize, *args, **kwargs, &block) return i end end class Test < SuperClass def initialize puts "No args here" end end SuperClass类基本上“重新实现”默认的new方法,以便在initialize之前进行一些额外的initialize 。 现在,以下工作正常: t = Test.allocate t.send(:initialize, *[], **{}, &nil) 但是,这不是: t = Test.new ArgumentError:参数个数错误(1表示0) from(pry):7:在`initialize’中 它在SuperClass失败了: i.send(:initialize, […]

rails上的ruby AbstractController :: Helpers :: MissingHelperError:缺少帮助文件助手//

我刚从另一台计算机上克隆了我正在处理的存储库。 当我尝试通过运行’rake db:migrate’进行数据库迁移时,我收到以下错误: AbstractController::Helpers::MissingHelperError: Missing helper file helpers//users/psmith/projects/clie/projectlion/app/helpers/application_helper.rb_helper.rb 我已经在这里尝试了最常见的答案: Rails:AbstractController :: Helpers :: MissingHelperError – 缺少帮助文件application_helper.rb_helper.rb 它说要在终端中执行以下操作: cd ~ mv projects projects1 mv projects1 projects 我也尝试过: sudo ln -s Users users 但这对我不起作用。 我该怎么办? 更新: 这是我的堆栈跟踪: ps-MacBook-Pro:projectlion psmith$ rake db:migrate rake aborted! AbstractController::Helpers::MissingHelperError: Missing helper file helpers//users/psmith/projects/clie/projectlion/app/helpers/application_helper.rb_helper.rb /Users/psmith/.rvm/gems/ruby-2.2.0/gems/actionpack-4.1.4/lib/abstract_controller/helpers.rb:154:in `rescue in block in modules_for_helpers’ /Users/psmith/.rvm/gems/ruby-2.2.0/gems/actionpack-4.1.4/lib/abstract_controller/helpers.rb:151:in `block in […]

ruby块内的赛璐珞异步不起作用

尝试在我的工作示例中实现Celluloid 异步似乎表现出奇怪的行为。 这里我的代码看起来 class Indefinite include Celluloid def run! loop do [1].each do |i| async.on_background end end end def on_background puts “Running in background” end end Indefinite.new.run! 但是当我运行上面的代码时,我从未看到过“ 在后台运行 ” 但是,如果我睡觉 ,代码似乎工作。 class Indefinite include Celluloid def run! loop do [1].each do |i| async.on_background end sleep 0.5 end end def on_background puts “Running in background” […]

关键字参数在Ruby中解包(splat)

下面发生的事情对我来说似乎有点奇怪。 def f(a, b) puts “#{a} :: #{b}” end f(*[1, 2], **{}) # prints “1 :: 2” hash = {} f(*[1, 2], **hash) ArgumentError: wrong number of arguments (3 for 2) f(*[1, 2], **Hash.new) ArgumentError: wrong number of arguments (3 for 2) 这是编译器优化function吗?

rails服务器无法启动; getaddrinfo:提供nodename或servname,或者未知(SocketError)

我还没有找到问题的解决方案,但是几天前有人已经问过同样的问题 – ( Rails Server Keeps Exiting(SocketError) ) 启动rails服务器后,系统返回一些我无法理解的错误。 要在我的Mac上安装Ruby on Rails,我做了http://railsapps.github.io/installrubyonrails-mac.html上列出的所有内容。 这是我的终端回复: $ rails server => Booting WEBrick => Rails 4.2.0 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server [2015-02-28 02:14:46] INFO WEBrick 1.3.1 [2015-02-28 02:14:46] INFO ruby 2.2.0 (2014-12-25) [x86_64-darwin14] Exiting […]

AWS Elastic Beanstalk – 如何将现有环境从Ruby 2.1升级到Ruby 2.2

AWS Elastic Beanstalk – 无法克隆最新平台或eb upgrade从Ruby 2.1 eb upgrade到Ruby 2.2 我一直在粉碎这个。 早在5月,AWS宣布他们的Ruby Elastic Environments现在提供Ruby-2.2(例如ruby-2.2-(passenger-standalone)或ruby-2.2-(puma) )。 我无法将现有的ruby-2.1环境升级到ruby-2.2。 看来我必须完全重建它们……这看起来很傻? 有没有其他人经历过这个? 我只是缺少一些简单的东西吗? 额外的信息 在我的舞台和制作环境中,我一直兴高采烈地使用ruby-2.1-(passenger-standalone)几个月。 现在我想将它们升级到最新的ruby-2.2平台。 AWS文档说这非常简单,实际上他们的一些文档甚至似乎表明你可以使用eb clone –update 。 我正在使用的EB CLI 3.4.5中不存在该标志:(此外,Web控制台具有Clone with Latest Platform选项菜单项,但它已被禁用。 看来,我所能做的就是到达最新的Ruby-2.2实例,就是从头开始创建一个全新的环境。 这非常令人讨厌。 所以这就是我尝试过的…… $ eb status Environment details for: staging Application name: xyz Region: us-west-2 Deployed Version: fbe7 Environment ID: someId Platform: 64bit […]

当使用::?指定模块时,为什么Ruby不能在更高的范围内查找类?

我只是坚持了一段时间。 拿这个基础: module Top class Test end module Foo end end 后来,我可以通过这样做在Foo中定义扩展Test的类: module Top module Foo class SomeTest < Test end end end 但是,如果我尝试通过使用::来指定模块来最小化缩进: module Top::Foo class Failure < Test end end 这失败了: NameError:未初始化的常量Top :: Foo :: Test 这是一个错误,还是只是Ruby解析变量名称的逻辑结果?