首页 > 解决方案 > 使用布尔值设置 Rails 选择框的样式

问题描述

我试图在 Stack 上找到我要查找的内容,但无济于事,我试图将自定义样式类添加到 rails 中的选择表单字段,但我正在删除..有人可以帮帮我吗?

<%= f.select(:allow_profile_update_no_pw, options_for_select([['Active', true], ['Inactive', false]], {:selected => current_user.allow_profile_update_no_pw, :class => "someClass"}),:prompt => "Select") %>

这是我将它放在任何 {} 中时遇到的错误

wrong number of arguments (given 3, expected 1..2)

移出那些我无法得到任何结果的人。

标签: ruby-on-rails

解决方案


select方法是这样定义的

select(object, method, choices = nil, options = {}, html_options = {}, &block)

在您的情况下,您需要将class键值对移动到下一个参数,即 under html_options,以及其他一些修复,它应该看起来像这样

<%= f.select :allow_profile_update_no_pw, options_for_select([['Active', true], ['Inactive', false]], current_user.allow_profile_update_no_pw),{ :prompt => "Select"}, { :class => "someClass" } %>

https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select


推荐阅读