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

Rails搜索开发:select传递params时参数数量错误问题排查

Rails select表单参数数量错误修复

你遇到的wrong number of arguments (given 5, expected 1..4)错误,是因为form.select方法最多接受4个参数,你把:selected作为独立参数传递,导致参数总数超标。

正确的写法是将:selected选项合并到第三个参数的选项哈希中,代码如下:

<%= form.select :listing_type, Listing.listing_types.keys, {selected: params[:listing_type]}, {class: "form-select form-select-lg", id: "floatingSelect"} %>

关键说明

  • form.select的参数结构为:select(method, choices, options = {}, html_options = {})
  • 选中值、提示文本这类逻辑选项要放在第三个options哈希里
  • class、id这类HTML属性要放在第四个html_options哈希里

内容的提问来源于stack exchange,提问作者Joe Bloggos

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 08:40:56