ruby-on-rails – 更改模型的输入名称

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 更改模型的输入名称前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用ActiveAttr:
class Filter
  include ActiveAttr::Model
  attribute term
  # Overriding to_key,to_param,model_name,param_key etc doesn't help :(
end

class SpecialFilter < Filter
end

如何覆盖ActiveModel以生成所有子类的(相同)预定义输入名称

= form_for SpecialFilter.new,url: 'xx' do |f|
  = f.text_field :term

因此,而不是< input name ='special_filter [term]'/>我需要得到< input name ='filter [term]'/>

注意:场景更复杂(使用simple_form和radio / checkBoxes / dropdown等),因此请不要建议更改类的名称或类似的解决方法.我确实需要使用表单生成器使用的一致名称.

解决方法

试试这个 :
= form_for SpecialFilter.new,as: 'filter',url: 'xx' do |f|
  = f.text_field :term
原文链接:https://www.f2er.com/ruby/266585.html

猜你在找的Ruby相关文章