Evan Johnson
Evan Johnson

Reputation: 1454

Fake select field for Simple Form

I'm using Simple Form, and I have a few fields that are not associated with my model. I found using this fake field option to be a good solution:

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

I thought this was cleaner than adding an attr_accessor value for my fields, and it works great for text input fields. I'm hoping to accomplish the same thing with a Select Field.

I found this thread, but I couldn't find anything else:

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

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

Upvotes: 6

Views: 2434

Answers (2)

dft
dft

Reputation: 648

If your just trying to get the name='whatever' instead of having name='model[whatever]' I've found it easiest to just specify the name attribute in input_html { name: 'whatever', id: 'whatever' } hash which over rides the default model[attribute].

Otherwise you could create a fake_select_input.rb which would be similar to fake_input.rb but obviously use a select_tag instead and do something like as: :fake_select

Upvotes: 2

dgilperez
dgilperez

Reputation: 10796

Assuming you'll use that "fake select" for UI purposes (probably as a mean to modify the form fields to present the user using Javascript?) and you don't want to deal with the value in the controller, you could just use select_tag with any field name, instead of the simple_form f.input. The value will be sent to the server, but it will be outside the model params, so you can just ignore it in the controller.

If I misunderstood your question, please clarify.

Upvotes: 2

Related Questions