Ruby on rails 比较数字时出现意外问题

Ruby on rails 比较数字时出现意外问题,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我正在编写一个helper方法,它根据两个数字是否相等向元素添加一个类。我的代码如下: <% for note in @company.convertible_notes.each %> <% if note.id %> <li class="tab <%= note_nav(params, note.id) %>"><%= link_to "#{note.security_series} #{note.securit

我正在编写一个helper方法,它根据两个数字是否相等向元素添加一个类。我的代码如下:

<% for note in @company.convertible_notes.each %>
    <% if note.id %>
        <li class="tab <%= note_nav(params, note.id) %>"><%= link_to "#{note.security_series} #{note.security_class} Note", convertible_note_convertible_notees_path(note) %></li>
    <% end %>
<% end %>
现在令人惊讶的是,我无法得到表达式
note.to_s==params[:converible_note\u id].to_s
来注册true。即使我知道两个被比较的数字都是“1”。我用我的日志检查了它:

logger.debug "are they equal? #{note.to_s==params[:converible_note_id].to_s} note.id is #{note} note params are #{params[:convertible_note_id]}"
这将生成以下日志条目:

他们平等吗?false note.id为1注释参数为1

我猜它们是两种不同的类型,但考虑到我已经将它们都转换成了_,我不知道这会是个什么问题。我在其他模型的一些组合上使用了完全相同的技术,并且完全没有错误。你知道会发生什么吗

提前感谢

看看你的测试

"are they equal? #{note.to_s==params[:converible_note_id].to_s} note.id is #{note} note params are #{params[:convertible_note_id].to_s}"

:converible\u note\u id
:converible\u note\u id
是其他键,类型错误

如果其中一个变量包含非打印字符,也可以获得该输出:

note = "1\000"

params = {
  convertible_note_id: 1
}

puts "are they equal? #{note.to_s==params[:convertible_note_id].to_s}"
puts "note.id is #{note} note params are #{params[:convertible_note_id]}"

--output:--
are they equal? false
note.id is 1 note params are 1
要查看字符串中的实际内容,应始终使用inspect():


这不是第一个条件中的一个输入错误吗?:
如果参数[:controller]=“convertible\u notes”
-请注意“notes”。。。这将使整个条件总是失败…@borama不,这是对的,尽管有点奇怪
note = "1\000"

params = {
  convertible_note_id: 1
}

puts "are they equal? #{note.to_s==params[:convertible_note_id].to_s}"
puts "note.id is #{note} note params are #{params[:convertible_note_id]}"

--output:--
are they equal? false
note.id is 1 note params are 1
p note.to_s, params[:convertible_note_id].to_s

--output:--
"1\u0000"
"1"