Rails 4: Change 'name' attribute of Simple Form input

sixty4bit picture sixty4bit · Jul 5, 2015 · Viewed 10.6k times · Source

I am using Simple Form in a Rails 4 app for a @user object and have the line...

= f.input :entity_name

This generates the HTML name='user[entity_name]' inside the input tag. I would like to change it because of some custom processing I am doing in the controller but haven't found a way to do so.

I've tried changing the line to...

= f.input :entity_name, name: 'entity[name]'

...but this doesn't seem to affect the generated HTML at all. So far I haven't found anyone else with this question on Google/Stack Overflow.

Does anyone know if/how it's possible to change the name attribute through the Simple Form helper?

Thanks in advance!

Answer

max picture max · Jul 5, 2015

The trick is using the input_html option.

= f.input :entity_name, input_html: { name: 'entity[name]' }