I have a Rails application and I'm using jQuery to query my search view in the background. There are fields q (search term), start_date, end_date and internal. The internal field is a checkbox and I'm using the is(:checked) method to build the url that is queried:

$.getScript(document.URL + "?q=" + $("#search_q").val() + "&start_date=" + $("#search_start_date").val() + "&end_date=" + $("#search_end_date").val() + "&internal=" + $("#search_internal").is(':checked'));

Now my problem is in params[:internal] because there is a string either containing "true" or "false" and I need to cast it to boolean. Of course I can do it like this:

def to_boolean(str)
     return true if str=="true"
     return false if str=="false"
     return nil
end

但是我想一定有一种更Ruby 的方式来处理这个问题!是不是有.?

推荐答案

据我所知,没有一种内置的方式可以为布尔人演奏弦乐,

def to_boolean(str)
  str == 'true'
end

Jquery相关问答推荐

如何定位 prism.js 脚本中包含的代码?

添加动态表单后,Select2 消失

如何使用 jQuery 获取 div 的当前类?

你如何获得文本区域中的光标位置?

根据弹出框相对于窗口边缘的 X 位置更改 Bootstrap 弹出框的位置?

$(document).on('click', '#id', function() {}) 与 $('#id').on('click', function(){})

如何将参数传递给 JavaScript 中的匿名函数?

使用 jQuery 获取选定的选项 id

jQuery UI 中的 disableSelection 用于什么?

停止输入/书写后如何触发输入文本中的事件?

jquery在加载时获取iframe内容的高度

更改占位符文本

.apply jQuery 函数是什么?

如何测试 URL 字符串是绝对的还是相对的?

数据表日期排序dd/mm/yyyy问题

jQuery位置href

如果一个元素正在被动画,我如何用 jQuery 找出?

请求的资源错误中不存在Access-Control-Allow-Origin标头

如何禁用由子元素触发的 mouseout 事件?

如何判断值是否为 JSON 对象?