ruby-on-rails - Ruby - 没有将 false 隐式转换为字符串
问题描述
我在下面解释的代码中收到以下错误。“没有将 false 隐式转换为字符串”
def search_form(target, search)
raw("<span class='no_print'>" <<
form_tag('/' + target + '/', :method => :get) <<
text_field_tag(:search, search) <<
submit_tag('Search', data: { disable_with: "Searching..." }) <<
!search.nil? ? link_to('Clear', root_path, class: 'clearingLink') : "" <<
'</form>' <<
'</span>' <<
form_focus('search'))
end
!search.nil? ? link_to('Clear', root_path, class: 'clearingLink') : "" <<
This is the line I have added recently. Can anyone please let me know what is the wrong with this ?
解决方案
"str" << false
是您遇到的错误的最小示例。该错误是与运算符优先级有关的误解的结果。值得注意的是,?:
的优先级低于<<
; 所以
a << b ? c : d << e
(b
你的在哪里!search.nil?
)评估为
(a << b) ? c : (d << e)
虽然您希望它会评估为
a << (b ? c : d) << e
解决方案:添加括号以确保所需的评估顺序。
推荐阅读
- javascript - 页面之间的信息丢失
- javascript - 在 NextJS 中未使用 axios 拦截器定义 localStorage
- python - 在数据框中计算滚动和的更快方法
- ios - Swift 实例成员和函数调用,有人可以澄清一下吗?
- r - 根据R中另一列的最小值获得一列的对应值
- sql - 使用多行子查询
- python - PyTorch:将预训练模型从 3 个 RGB 通道更改为 4 个通道后出现“ValueError:无法优化非叶张量”
- c# - Really struggling to get how WiX works
- c++ - C++ How to stop private member variables constructor being called. It supposed to reference another object
- android - Why the Facebook login popup opened on an external web browser app on my android device