小编Alv*_*Lee的帖子

在Rails ActiveRecord中分配给布尔字段时,值如何转换?

我的问题的简短版本

在Rails ActiveRecord中,如果我有一个布尔字段并且我给它分配了类似abc"或"的东西2,那么它会立即转换为false.该值1将转换为true,并nil保持为nil.为什么会这样?我在哪里可以找到解释此行为的Rails文档(或Ruby文档)?

我的问题的长版本

我很难理解Rails如何处理Boolean为Rails模型中的字段赋值的尝试.例如,假设我有一个Website模型,其中包含一个String被调用:domainBoolean字段和一个被调用的字段:can_ssl.

我的迁移看起来像这样:

class CreateWebsites < ActiveRecord::Migration
  def change
    create_table :websites do |t|
      t.string :domain
      t.boolean :can_ssl, :default => false

      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

在我的模型文件中,我添加了一些验证规则,所以它看起来像这样:

class Website < ActiveRecord::Base
  validates :domain, :presence => true
  validates :can_ssl, :inclusion =>  { :in => [true, false] }
end
Run Code Online (Sandbox Code Playgroud)

很简单.基于我做了什么,我期待:can_ssl只能设置为值true …

activerecord boolean ruby-on-rails

9
推荐指数
2
解决办法
5643
查看次数

在Capybara中,确实:或者Capybara.exact适用于have_selector?

have_selector在尝试匹配文本完全匹配的元素时,我很难使用Capybara .我知道这可以使用正则表达式完成,但是我阅读Capybara的博客文章让我相信我可以使用:exactarg或set Capybara.exact = true.我目前正在使用Capybara 2.2.1.这是我有的:

假设我有一个页面(称为"测试页面").在这个页面是这样的:

<div id="my_div">abcdef</div>
Run Code Online (Sandbox Code Playgroud)

我有一个Cucumber功能测试,看起来像这样:

Feature: Test for exact text matches in have_selector
Scenario:
  Given I am on the test page
  Then I should get a partial text match "bcde" in my div
  And I should get a partial text match "abcdef" in my div
  And I should get an exact text match using regexp "abcdef" in my div
  And I should not get an exact text …
Run Code Online (Sandbox Code Playgroud)

ruby cucumber capybara

3
推荐指数
1
解决办法
2995
查看次数

标签 统计

activerecord ×1

boolean ×1

capybara ×1

cucumber ×1

ruby ×1

ruby-on-rails ×1