小编Rya*_*Mes的帖子

如何测试枚举操作方法在创建或保存之前验证对象?

我试图测试会Lead.new(params).active!引发错误.最好的方法是什么?

class Lead < ActiveRecord::Base
  enum status: { stale: 0, active: 1, converted: 2 }

  validate  :existing_lead, on: :create

  private

  def existing_lead
    if new_record? && (stale? || converted?)
      errors.add(:status, "invalid for new leads") 
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我知道我可以手动设置枚举值,然后测试valid?我实例化的对象,但我希望有一种方法可以测试,stale!converted!在调用时保存到数据库.

ruby rspec ruby-on-rails ruby-on-rails-4

5
推荐指数
1
解决办法
1693
查看次数

collection_select和options_for_select不起作用

我正在尝试使用自定义数据创建一个collection_select选项.我知道如何使用自定义数据属性创建选项,但是当我尝试将这些选项添加到我的collection_select时,我的代码会中断,无论我做什么.

以下代码有效

<%= f.collection_select :tag_ids, Tag.all, :id, :name, {}, {multiple: true} %>
Run Code Online (Sandbox Code Playgroud)

然后我将其修改为,并在下面给出错误

<%= f.collection_select(:tag_ids, options_for_select(Tag.all.collect{|t| [t.name, t.id]}), {}, {multiple: true}) %>

undefined method `map' for #<ActiveSupport::SafeBuffer:0x00000102df6648>
Run Code Online (Sandbox Code Playgroud)

我用谷歌搜索并尝试了很多变化,但我希望有人可以帮助我形成这一点.

我知道我的代码不包含数据属性,但我简化了我的例子.

ruby-on-rails-4

5
推荐指数
1
解决办法
1184
查看次数

rspec接收方法无法按预期工作

我无法理解为什么以下代码在运行我的测试时在rspec中不起作用.

Lead.update_status(2,2,true)
expect(Lead).to receive(:update_status).with(2, 2, true)
Run Code Online (Sandbox Code Playgroud)

我也试过了

Lead.update_status(2,2,true)
expect_any_instance_of(Lead).to receive(:update_status).with(2, 2, true)
Run Code Online (Sandbox Code Playgroud)

错误II得到的是

(<Lead(id: integer, contact_id: integer, course_presentation_id: integer, status: integer, created_by_user_id: integer, updated_by_user_id: integer, created_at: datetime, updated_at: datetime, mailchimp_status: integer) (class)>).update_status(2, 2, true)
           expected: 1 time with arguments: (2, 2, true)
           received: 0 times with arguments: (2, 2, true)
Run Code Online (Sandbox Code Playgroud)

请注意,如果我将"test"添加到我的代码中,则会打印出来,因此我知道update_status实际上正在运行.

有任何想法吗?谢谢.

ruby rspec ruby-on-rails ruby-on-rails-4

4
推荐指数
1
解决办法
2566
查看次数

带有rails活动记录的sqlite boolean't'和'f'

我在开发时使用mysql on production和sqlite3.

在开发时查询我的数据库,例如

@follow_ups = FollowUp.where(is_complete: false)
Run Code Online (Sandbox Code Playgroud)

我在控制台中获得了下面的sql

SELECT "follow_ups".* FROM "follow_ups"  WHERE "follow_ups"."is_complete" = 'f'
Run Code Online (Sandbox Code Playgroud)

sqlite将'f'计算为truthy值,因此不返回follow_ups.is_complete = false.在数据库中,它们存储为true/false.从我的调查中我发现.

https://github.com/rails/rails/issues/10720
Rails 3 SQLite3 Boolean false

我应该怎么做才能使我的布尔过滤器工作?我原本以为这会发生在更多人身上.

见下面的架构

  create_table "follow_ups", force: true do |t|
    t.integer  "contact_id"
    t.integer  "owner_id"
    t.datetime "due_date"
    t.string   "comment"
    t.boolean  "is_complete",        default: false
    t.integer  "created_by_user_id"
    t.integer  "updated_by_user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
Run Code Online (Sandbox Code Playgroud)

查看下面的数据和插入 - 通过rails控制台完成.作为评论中的要求.

[30] pry(main)> FollowUp.create(contact_id: 1, due_date: Time.now, is_complete: true)
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "follow_ups" ("contact_id", "created_at", "due_date", "is_complete", "updated_at") VALUES …
Run Code Online (Sandbox Code Playgroud)

sqlite ruby-on-rails-4

4
推荐指数
1
解决办法
2082
查看次数

溢出:隐藏在桌面上导致底部和右边框消失

正如标题所示,当我使用overflow:hidden时,桌面边框被切断了.请参阅下面的代码.

    <style>
        table {
            border-collapse: collapse;
        }

        table {
            border: 1px solid black;
            overflow: hidden;
        }

    </style>

    <body>
        <table>
            <thead>
                <tr>
                    <th>header 1</th>
                    <th>header 2</th>
                    <th>header 3</th>

                </tr>
            </thead>
            <tbody>
                <tr>
                    <th>side header</th>
                    <td>data 1</td>
                    <td>data 2</td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <th>footer header</th>
                    <td>footer 2</td>
                    <td>footer 3</td>
                </tr>
            </tfoot>
        </table>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我简化了我的例子.我可能会失去边界崩溃的风格,这将纠正这一点,但我需要那种风格.如果没有它,我的代码会变得混乱.

作为一个临时解决方案,我发现我可以使用下面的CSS来破解它,但我不是黑客的拥抱者!

.borderhack:after {
            content: '\00a0';
            position: absolute;
            border: 1px solid black;
            left: -2px;
            top: -2px;
            width: 100%;
            height: 100%;
            z-index: -10;
        }

        table {
            position: relative;
        } …
Run Code Online (Sandbox Code Playgroud)

html css

3
推荐指数
2
解决办法
4937
查看次数

Rails 4.1是枚举功能吗?

我只是想尝试使用新的rails enum做最简单的事情(我知道它还处于测试阶段)

无论如何,我正在尝试使用rails控制台来拉取对象上的所有枚举(因为它不能在代码中工作),例如

class Interaction < ActiveRecord::Base
  enum outcome_type: { hot: 1, neutral: 2, cold: 3 }
end
Run Code Online (Sandbox Code Playgroud)

然后我运行rails控制台并尝试Interaction.outcome_types

根据文档,这应该是相当简单和直接的.

http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

我只得到错误:/

NoMethodError: undefined method `outcome_types' for #<Class:0x007fb5e595ebb8>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

enums ruby-on-rails

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

状态和道具是否可变?

假设我有一个react组件 - 一个按钮 - 当我点击它时会增加一个值.

例如

var Component = React.createClass({
    getInitialState: function() {
        return {count: 0}
    },

    increment: function() {
        this.setState({count: this.state.count + 1})
    },

    render: function() {
        return (<button onClick={this.increment}>{this.state.count}</button>);
    }
})

React.render(<Component />, document.getElementById('react-container'));
Run Code Online (Sandbox Code Playgroud)

国家是可变的!

我可以用道具做类似的事情

var Component = React.createClass({
    increment: function() {
        this.setProps({count: this.props.count + 1})
    },

    render: function() {
        return (<button onClick={this.increment}>{this.props.count}</button>);
    }
})

React.render(<Component count={0}/>, document.getElementById('react-container'));
Run Code Online (Sandbox Code Playgroud)

国家是可变的!

我检查过的一些资源说道具是不可改变的,但是然后去做类似的事情setProps.不同的资源互相矛盾.这使我很难掌握状态和道具之间的区别.

道具是否可变?如果没有,为什么我可以改变它们?似乎改变道具不是好习惯,这是真的吗?如果是,为什么setProps存在?

reactjs

3
推荐指数
2
解决办法
5982
查看次数

为什么`JSON.parse`没有抛出异常?

我们一直在使用JSON.parse一段时间,我们刚刚更新到最新版本.

我们收到以下错误.

功能是否已更新为返回nil而不是抛出ParserError?

2.3.0 :001 > gem 'json', '2.0.2'
 => true
2.3.0 :002 > require 'json'
 => true
2.3.0 :003 > JSON.parse("null")
 => nil
Run Code Online (Sandbox Code Playgroud)
2.3.0 :001 > gem 'json', '1.8.3'
 => true
2.3.0 :003 > require 'json'
 => true
2.3.0 :004 > JSON.parse("null")
JSON::ParserError: 784: unexpected token at 'null'
  from /Users/ryannealmes/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/json/common.rb:156:in `parse'
  from /Users/ryannealmes/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/json/common.rb:156:in `parse'
  from (irb):4
  from /Users/ryannealmes/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
2.3.0 :005 >
Run Code Online (Sandbox Code Playgroud)

ruby json rubygems

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

验证令牌JWT [RSA]

一个collegue和我一直在试图了解如何JWT令牌验证令牌,但是从我们的阅读,我们似乎会混淆自己。

请有人可以帮助确认是否我的想法是正确的

  • 令牌使用私钥签名。签名是报头和有效载荷使用私有密钥加密的,并添加到JWT作为最后部分,签名的组合。
  • 为了验证令牌接收器可以使用公钥复制这一过程。他们加密头和有效载荷,看它是否一样的签名。请注意,这是不解密。该接收器没有解密令牌(这是我们不能确定的主要的东西)。-The接收机不能发行新的令牌,因为他们没有私有密钥来加密与一个新的令牌。

我看了两个RS256和HS256并仍在努力确认了我的思维,因此后期的智威汤逊文档。

cryptography digital-signature jwt

3
推荐指数
2
解决办法
1818
查看次数

rails 4.1枚举不从视图中保存

Rails 4.1提供了枚举.我查了一下它似乎在rails控制台上工作得很好.当我尝试通过我的控制器将数据从视图持久化到数据库时,我收到以下错误

"注册"不是有效的stream_type

以下是我的课

class Stream < ActiveRecord::Base
  enum stream_type: { sales: 1, registration: 2, student: 3 }

  belongs_to :course_presentation

  has_many :subscriptions
  has_many :contacts, :through => :subscriptions

  validates :course_presentation_id, :stream_type, presence: true 
end
Run Code Online (Sandbox Code Playgroud)

下面是我用来保存的代码

@stream = Stream.new(stream_params)

def stream_params
      params.require(:stream).permit(:stream_type, :course_presentation_id, :is_active, :created_by_user_id, :updated_by_user_id)
    end
Run Code Online (Sandbox Code Playgroud)

下面是视图代码

<%= f.label :stream_type %><br>
    <%= f.collection_select :stream_type, StreamType.order(:name), :name, :name, include_blank: "<-- select -->" %>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我无法让它发挥作用

enums ruby-on-rails ruby-on-rails-4

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

$ unset如何工作?

mongo文档中,取消设置字段$unset.我不太清楚它是如何工作的,但它看起来应该很简单.

The following operation uses the $unset operator to remove the tags field:

db.books.update( { _id: 1 }, { $unset: { tags: 1 } } )
Run Code Online (Sandbox Code Playgroud)

在设定未设置的内容时会出现混乱.1$unset条款的价值是多少?

mongodb

0
推荐指数
1
解决办法
3077
查看次数