Ruby on rails Rails逻辑在'false'结果上不触发

Ruby on rails Rails逻辑在'false'结果上不触发,ruby-on-rails,Ruby On Rails,作为API接收哈希数组 @request['clients'].each do |client| 正在对每个客户端属性执行验证。然而,rails逻辑无法激发错误的语句,因此忽略了它们。验证示例: def client_type_ok if @this_client['type'] == "ADT" || @this_client['type'] == "CHD" true else false @error_code_39 = true

作为API接收哈希数组

@request['clients'].each do |client|
正在对每个
客户端
属性执行验证。然而,rails逻辑无法激发错误的语句,因此忽略了它们。验证示例:

  def client_type_ok
    if @this_client['type'] == "ADT" || @this_client['type'] == "CHD"
      true
    else
      false
      @error_code_39 = true
    end
  end
控制器操作仅在满足真实条件时执行:

if client_type_ok && client_type_ok
然而,Rails.logger正在明确确认此情况正在通过,尽管
false

Rails.logger.info !@this_client['type'].blank?
Rails.logger.info  @this_client['type']
Rails.logger.info "not"
Rails.logger.info  @this_client['type'] != "ADT"
Rails.logger.info "ADT"
Rails.logger.info @this_client['type'] == "ADT"
他回来了

true
APT
not
true
ADT
` `
底部生成为空白。用
p
替换Rails.logger时也会发生同样的情况。此操作的所有逻辑都是忽略错误结果。虽然我可以尝试设计完全真实案例的处理案例,但这既不方便又违反直觉


因此,似乎有一种元功能阻碍了对虚假案件的处理。如何才能找到这一点?可以跟踪Rails.logger逻辑吗?

这里没有返回false

  def client_type_ok
    if @this_client['type'] == "ADT" || @this_client['type'] == "CHD"
      true
    else
      false # this is not doing anything. Simply ignored.
      @error_code_39 = true # result of this assignment will be true.
                            # and it also is the last expression of the if
                            # so it becomes the implicit return value of the method
    end
  end

你不会在那里返回错误

  def client_type_ok
    if @this_client['type'] == "ADT" || @this_client['type'] == "CHD"
      true
    else
      false # this is not doing anything. Simply ignored.
      @error_code_39 = true # result of this assignment will be true.
                            # and it also is the last expression of the if
                            # so it becomes the implicit return value of the method
    end
  end
正如Sergio在上面的回答中提到的,yur方法的返回值在else条件下为真。您需要交换位置,或者可以重写上述方法

def client_type_ok
    return true if %w(ADT CHD).include?(@this_client['type'])

    @error_code_39 = true
    false
  end
正如Sergio在上面的回答中提到的,yur方法的返回值在else条件下为真。您需要交换位置,或者可以重写上述方法

def client_type_ok
    return true if %w(ADT CHD).include?(@this_client['type'])

    @error_code_39 = true
    false
  end

如果正在处理的值是
APT
,这是否应该返回false?@Jerome:没有。你看到我的评论了吗?哦,那么
错误\u code
也被代理为
客户端类型\u ok
的状态。换言之,要想有效,它们需要被颠倒。我假设,因为有一个声明,它将被解释为一个独立的实体;愚蠢的我。@Jerome:是的,让他们交换位置。如果正在处理的值是
APT
,这不应该返回false吗?@Jerome:不。你看到我的评论了吗?哦,那么
错误\u code
也被代理为
客户端类型\u ok
的状态。换言之,要想有效,它们需要被颠倒。我假设,因为有一个声明,它将被解释为一个独立的实体;我真傻。@Jerome:是的,把他们的位置换了。