Fake selection box for simple form

I am using Simple Form and I have several fields that are not related to my model. I found that using this fake field is a good solution:

https://github.com/plataformatec/simple_form/wiki/Create-a-fake-input-that-does-NOT-read-attributes

I thought it was cleaner than adding the attr_accessor value for my fields, and it works great for text input fields. I hope to do the same with the selection box.

I found this thread but found nothing else:

https://github.com/plataformatec/simple_form/issues/747

Has anyone found a similar option for entering Fake Select? Thanks!

+6
source share
2 answers

Assuming you will use this “fake choice” for UI purposes (perhaps as a means to change form fields to represent the user using Javascript?), And you don’t want to deal with the value in the controller, you can simply use select_tag with any field name instead of simple_form f.input . The value will be sent to the server, but it will be outside the model parameters, so you can simply ignore it in the controller.

If I misunderstood your question, please clarify.

+2
source

If you are just trying to get name='whatever' instead of name='model[whatever]' , it was easiest for me to simply define the name attribute in the input_html { name: 'whatever', id: 'whatever' } hash, which would exceed the default value model[attribute] .

Otherwise, you could create fake_select_input.rb that would look like fake_input.rb , but obviously use select_tag and do something like as: :fake_select

+2
source

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


All Articles