You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置best_in_place复选框的默认勾选状态及禁用属性

Best In Place 复选框默认勾选并禁用的实现方法

Best In Place 支持实现复选框默认勾选且禁用的需求,具体可通过以下方式结合实现:

  • 默认勾选状态:取决于对应模型字段的初始值。若要默认勾选,确保model的:attribute字段初始值为true——可以在数据库层面设置字段默认值为true,或者在实例化模型时主动赋值为true。Best In Place会根据模型字段的当前值自动渲染复选框状态。

  • 禁用编辑功能:在best_in_place调用中添加disabled: true参数,即可让组件失去可编辑交互能力。

修改后的完整代码示例:

best_in_place model, :attribute,
               url: model_path(model),
               as: :checkbox,
               collection: { false: "<i class='fa fa-square-o'></i>", true: "<i class='fa fa-check-square-o'></i>" },
               disabled: true

如果需要动态控制禁用状态,还可以结合条件判断实现:

best_in_place model, :attribute,
               url: model_path(model),
               as: :checkbox,
               collection: { false: "<i class='fa fa-square-o'></i>", true: "<i class='fa fa-check-square-o'></i>" },
               disabled: model.some_business_condition? # 根据业务条件动态设置禁用状态

内容的提问来源于stack exchange,提问作者Jonathon Nordquist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 22:58:09