Change model login

Using 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 

How to override ActiveModel to generate (identical) predefined input names for all subclasses ?

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

So, instead of <input name='special_filter[term]' /> I need to get <input name='filter[term]' />

NOTE. The script is much more complicated (using simple_formats and radio stations / flags / drop-down lists, etc.), so please do not suggest changing the class name or similar workarounds. I really need to have a consistent name that will be used by the form builder.

+6
source share
2 answers

Try the following:

 = form_for SpecialFilter.new, as: 'filter', url: 'xx' do |f| = f.text_field :term 
+8
source

As Divya Bhargov said, if you look at the source code, you will find that the inner call stack should end, as shown below.

  # actionpack/lib/action_view/helpers/form_helper.rb ActiveModel::Naming.param_key(SpecialFilter.new) # activemodel/lib/active_model/naming.rb SpecialFilter.model_name 

So, if you really want to do this at your model level, you need to override model_name in your class.

 class SpecialFilter < Filter def self.model_name ActiveModel::Name.new(self, nil, "Filter") end end 

The parameter for this initializer is ActiveModel :: Name: klass, namespace = nil, name = nil .

But model_name also used somewhere else, such as error_messages_for, so use this with caution.

+5
source

Source: https://habr.com/ru/post/924823/


All Articles