Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 541
Evan Johnson Avatar asked Aug 04 '14 05:08

Evan Johnson


2 Answers

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.

like image 187
dgilperez Avatar answered Sep 29 '22 13:09

dgilperez


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

like image 33
dft Avatar answered Sep 29 '22 15:09

dft