Change input name of model
Asked Answered
R

2

7

Using the 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 do I override the ActiveModel to generate the (same) 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 scenario is way much more complicated (with simple_form and radios/checkboxes/dropdowns etc), so please do not suggest to change the name of the class or similar workarounds. I really do need to have the consistent name to be used by the form builder.

Remington answered 7/9, 2012 at 7:1 Comment(0)
I
8

Try this :

= form_for SpecialFilter.new, as: 'filter', url: 'xx' do |f|
  = f.text_field :term
Initiate answered 7/9, 2012 at 7:22 Comment(3)
Thanks. That does the job. I'm still curious how to do it on the model (what do we need to override from ActiveModel?)Remington
I doubt this is happening at ActiveModel level. It seems to be at a form_helper level. See github.com/rails/rails/blob/… on how exactly the form is generated.Initiate
actually the default is on ActiveModel level, see github.com/rails/rails/blob/…Fishworm
P
6

As Divya Bhargov answered, if you take a look at the source code you'll find out the internal call stack should end up like 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 it in your model level, you need to override the model_name to your class.

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

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

But model_name is also used somewhere else such as error_messages_for, so do use this with care.

Praise answered 8/9, 2012 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.