【发布时间】:2022-08-10 16:39:05
【问题描述】:
我有一个旧的辅助方法定义为:
def feature(label, style = \'text\', inverse: false, **html_options)
由另一个助手调用,调用它:
def create_feature(level, html_options = {})
label, value = .....
feature(label, value, html_options)
end
在我的例子中:
(byebug) label
\"In progress\"
(byebug) value
:pending
(byebug) html_options
{ \"data-value\"=>\"pending\", :class=>\"mr-2\"}
我在我正在编辑的视图中调用它(在这种情况下我很乐意修改的唯一一点代码):
<%= create_feature(level, my_helper(:pending).merge({class: \'mr-2\'})) %>
其中my_helper 为html 元素生成data-value: pending 属性。
此代码的先前版本是:
<%= create_feature(level, class: \'mr-2\') %>
这有效,我现在需要添加带有来自my_helper 的额外属性的哈希,但我得到的是:
*** ArgumentError Exception: wrong number of arguments (given 3, expected 1..2)
奇怪的是,我创建了相同代码的虚拟版本,它工作得很好:
def feature(label, style = \'text\', inverse: false, **html_options)
pp html_options
end
def create_feature(level, html_options = {})
label, value = [\'in progress\', :pending]
feature(label, value, html_options)
end
create_feature(12, {hello: \'hello\', class: \'mr-2\'})
# {:hello=>\"hello\", :class=>\"mr-2\"}
-
\"我创建了相同代码的虚拟版本,它工作得很好\"– 如果是这样,错误的原因在您显示的代码之外。仔细查看错误消息及其堆栈跟踪。也许还有另一种方法叫你失踪。
标签: ruby-on-rails ruby