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

Rails中如何将按钮行为作为统一参数传递给partial?

错误原因

你尝试的写法报错核心有两个问题:

  1. Ruby 哈希构造语法不合法:{resend_verification_url, method: :put} 没有给 url 参数指定对应键,不符合 Ruby 哈希键: 值的基础结构要求
  2. button_to 参数规则不匹配:button_to 第二个参数是单独的 URL 位置参数,不属于关键字参数范畴,不能直接和method这类配置项混在一起做整体 splat 展开

可行实现方案

方案1:URL单独传递,其余配置打包(更易读)

调用侧代码:

<%= render partial: "shared/button", locals: {title: "Foo", url: resend_verification_url, opts: {method: :put} } %>

partial 侧代码:

<%= button_to title, url, **opts, class: "btn btn-primary btn-sm " %>

方案2:所有行为参数统一打包进单个变量

调用侧代码:

<%= render partial: "shared/button", locals: {title: "Foo", behaviour: {url: resend_verification_url, method: :put} } %>

partial 侧代码(先从哈希中提取 URL 作为位置参数,再展开剩余配置项):

<%= button_to title, behaviour.delete(:url), **behaviour, class: "btn btn-primary btn-sm " %>

内容的提问来源于stack exchange,提问作者Chris A

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 12:57:00