小编ste*_*ble的帖子

理解"||" 在Ruby中的if条件中的OR运算符

简而言之,为什么以下三条线的影响不一样?

if @controller.controller_name == "projects" || @controller.controller_name == "parts"

if @controller.controller_name == ("projects" || "parts")

if @controller.controller_name == "projects" || "parts"
Run Code Online (Sandbox Code Playgroud)

第一个给出了我想要的结果,但实际上有更多选项而不仅仅是项目和部分,使用该表单创建了一个详细的语句.另外两个更紧凑,但不会给我相同的结果.

ruby if-statement operators

37
推荐指数
4
解决办法
6万
查看次数

在包含任意数量的嵌套哈希和数组的哈希深处查找键/值对

Web服务返回一个包含未知数量的嵌套哈希的哈希,其中一些包含一个数组,该数组又包含未知数量的嵌套哈希值.

一些键不是唯一的 - 即存在于多个嵌套的哈希中.

但是,我真正关心的所有键都是独一无二的.

有没有什么事情我可以给顶级哈希一个关键,并且即使键值对深埋在这个泥潭里,也要回到它的价值?

(该网络服务是亚马逊产品广告API,它根据每个产品类别中允许的结果数量和搜索类型略微改变其提供的结果结构.)

ruby arrays hash amazon nested

35
推荐指数
6
解决办法
4万
查看次数

为什么我不能将PostgreSQL添加到我的路径中?

升级到Mavericks之后,PostgreSQL开始播放,每当我打电话时都会发出此错误消息pg_restore而不调用完整路径:

pg_restore: command not found
Run Code Online (Sandbox Code Playgroud)

如果我指定它的完整路径,但这显然不是最佳的:

/Applications/Postgres93.app/Contents/MacOS/bin/pg_restore --verbose --clean --no-acl --no-owner -h localhost -U steven -d db/testivate_development $file_path
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我尝试删除所有版本的PostgreSQL(使用Homebrew),然后安装Postgres.app.您可以确认这样做是这样的:

$ sudo find / -name pg_restore
/Applications/Postgres93.app/Contents/MacOS/bin/pg_restore
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
Run Code Online (Sandbox Code Playgroud)

要将PostgreSQL添加到我的路径,我尝试添加以下每行~/.bash_profile,~/bashrc~/.zshrc在每次尝试后重新启动:

export PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"
Run Code Online (Sandbox Code Playgroud)

...根据postgresapp.com文档,然后......

export PATH="/Applications/Postgres93.app/Contents/MacOS/bin:$PATH"
Run Code Online (Sandbox Code Playgroud)

......根据以下评论.

这些都没有解决我的问题.

postgresql macos

16
推荐指数
4
解决办法
2万
查看次数

如何在任何规范失败时自动生成Rspec save_and_open_page

我有...

/spec/spec_helper.rb:

require 'capybara/rspec'
require 'capybara/rails'
require 'capybara/dsl'

RSpec.configure do |config|
  config.fail_fast = true
  config.use_instantiated_fixtures = false 
  config.include(Capybara, :type => :integration)
end
Run Code Online (Sandbox Code Playgroud)

因此,只要任何规范失败,Rspec就会退出并向您显示错误.

在这一点上,我希望Rspec也自动调用Capybara的save_and_open_page方法.我怎样才能做到这一点?

Capybara-Screenshot看起来很有前途,但是虽然它将HTML和截图保存为图像文件(我不需要),但它不会自动打开它们.

rspec capybara

13
推荐指数
1
解决办法
9090
查看次数

跳转到ruby-debug中*my code*的下一行

在ruby-debug中,下一个命令将带您进入下一行要执行的代码,无论该行是在您自己的代码中,还是在rails框架中,还是在gem中.

是否有某种方法可以强制ruby-debug(1.9)跳转到它在代码中遇到的下一行而不停止它必须在路上执行的所有其他行?

ruby-on-rails ruby-debug

11
推荐指数
1
解决办法
2485
查看次数

如何用我的文件创建的新Git对象替换损坏的Git对象,这很好

在我的文件系统中,我的应用程序运行正常.所有测试都通过.

我是一个唯一的开发人员,使用Git主要使用Heroku以及备份.

由于腐败的物体,Git已停止工作.

我收到的错误看起来像这样:

steven-nobles-imac-200:drominay steven$ git push heroku master
error: inflate: data stream error (invalid distance too far back)
fatal: object 990ad2766afa2e2002eea265225ad160e73eacd2 is corrupted
error: pack-objects died with strange error
Run Code Online (Sandbox Code Playgroud)

(以及许多其他错误,都引用了损坏的对象.)

如何让Git抛弃损坏的对象并用我的应用程序的工作副本构建的新对象替换它们?

Git init 没有解决问题.

谢谢!

git

9
推荐指数
1
解决办法
5477
查看次数

在Rails中使用带有多态关联的"_type"列

我有一个Slot模型,属于Configuration模型以及另一个模型:

在db/migrate/... create_slots.rb中:

  t.references :slottable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)

在app/models/slot.rb中:

  belongs_to :slottable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)

在app/models/configuration.rb中:

  has_many :slots, :as => :slottable
Run Code Online (Sandbox Code Playgroud)

在rails控制台中,我得到:

  ruby-1.9.2-p180 :009 > Slot.last

   => #<Slot id: 69, slottable_id: 35, slottable_type: "configuration", number: 2, usage: "1GB 667MHz DDR2 SDRAM PC2-5300 SO-DIMM", created_at: "2011-08-09 12:12:25", updated_at: "2011-08-09 12:12:25"> 

   ruby-1.9.2-p180 :009 > Slot.last.slottable

   NameError: wrong constant name configuration
Run Code Online (Sandbox Code Playgroud)

我能想到的唯一解释是我误解了slottable_type列的使用.我有没有?

目前,在app/models/configuration.rb中:

slot = Slot.find_or_create_by_slottable_id_and_slottable_type(self.id, "configuration")
Run Code Online (Sandbox Code Playgroud)

polymorphism ruby-on-rails associations

8
推荐指数
1
解决办法
2243
查看次数

Heroku为什么认为一切都是最新的?

在Heroku我有一个生产应用程序,我知道有一个临时应用程序:

$ heroku list
=== My Apps
testivate
testivate-staging
Run Code Online (Sandbox Code Playgroud)

我有每个遥控器:

$ git remote -v
heroku  git@heroku.com:testivate.git (fetch)
heroku  git@heroku.com:testivate.git (push)
staging git@heroku.com:testivate-staging.git (fetch)
staging git@heroku.com:testivate-staging.git (push)
Run Code Online (Sandbox Code Playgroud)

我走了几天,部署打破了我的生产应用程序,所以我用了heroku rollback,最后创建了我现在正在使用的暂存应用程序,并将我的代码推送到临时应用程序,大概是git push staging master.(这是几天前,但我很确定这就是我所做的.)

它现在都在我的临时应用程序上工作,所以我正在尝试将我的代码推送到我的生产应用程序.

但是,Heroku告诉我,我的生产应用程序已经是最新的:

$ git branch
* master

$ git status
# On branch master
nothing to commit (working directory clean)

$ git add .
$ git add -u
$ git commit -m "trying to commit"
# On branch master
nothing to commit (working directory clean) …
Run Code Online (Sandbox Code Playgroud)

git heroku

8
推荐指数
1
解决办法
5625
查看次数

期待Rails文件中的tASSOC

我确定我在这里做了一些蠢事,但我看不到它.

我在应用程序视图中调用了breadcrumb方法.

app/helpers/breadcrumbs_helper.rb说:

module BreadcrumbsHelper

  def breadcrumb
    @crumb_list = []
    drominay_crumb_builder
    project_crumb_builder
    content_tag(:div, :id => "breadcrumbs", @crumb_list.map { |list_item| crumb_builder(list_item) })
  end

  def crumb_builder(list_item)
    if list_item == @crumb_list.last
      content_tag(:span, list_item['body'], :class => list_item['crumb'])
    else
      body = ["list_item['body']", "&nbsp;&#x2192;&nbsp;"].join
      link_to(body, list_item['url'], :class => list_item['crumb'])
    end
  end

  def drominay_crumb_builder
    list_item = Hash.new
    list_item['body'] = "Drominay"
    list_item['url'] = "root"
    @crumb_list << list_item
  end

  def project_crumb_builder
  end

end
Run Code Online (Sandbox Code Playgroud)

为什么我为什么会得到这个"期待tASSOC"错误?(无论如何,什么是tASSOC?)

steven-nobles-imac-200:drominay steven$ script/server
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails 2.2.2 …
Run Code Online (Sandbox Code Playgroud)

database methods ruby-on-rails

7
推荐指数
1
解决办法
1万
查看次数

为什么我使用Capybara-webkit获取空白页,而不使用标准(非JS)驱动程序

我的Rspec + Capybara集成测试工作正常,直到我开始尝试使用Capybara-Webkit与依赖Javascript的界面元素进行交互.

当我尝试使用Capybara-Webkit时,我会在save_and_open_line下面看到空白页面.为什么?

/spec/features/area_spec.rb:

require 'spec_helper'
describe "Area" do
  ...
  describe "Admin" do
    ...  
    before(:each) do
      Capybara.current_driver = :webkit
      @virginia = User.create(
        :username => "Virginia", 
        :password => "password", 
        :password_confirmation => "password", 
        :email => "example@example.com")
      @admin = Role.create(:name => "admin")    
      @assignment = Assignment.create(user_id: @virginia.id, role_id: @admin.id)
      visit login_path
      fill_in 'user_session_username', :with => @virginia.username
      fill_in 'user_session_password', :with => @virginia.password
      click_on 'submit_user_session'
      visit report_path(@report)
      save_and_open_page
      page.find('#nav_reports').trigger(:mouseover)
      ...
    end
    ...
  end
  ...
end
Run Code Online (Sandbox Code Playgroud)

/spec/spec_helper.rb:

require 'rubygems'
require 'spork'
require …
Run Code Online (Sandbox Code Playgroud)

rspec capybara capybara-webkit

6
推荐指数
2
解决办法
3078
查看次数