Custom id for input using simple form in Rails

Simple_form  provides a lot of handy html tags for forms. However, the autocreation of classes and id’s was giving me trouble because I couldn’t figure out how to give an element the id I wanted. It’s actually very simple.

input_html: { id: 'my_id'}

is used to give an id to the specific element. This can be a bit tricky when using collection_select, as it takes a bunch of inputs. I typically put the input_html: {} as the last input.

<%= f.input :item_id, collection: Supplier.order(:name), as: :grouped_select, group_method: :items, group_label_method: :name, label_method: :name, value_method: :id, :prompt => 'Choose an existing Item', input_html: { id: 'quantity_item_select'} %>

Check the documentation for collection_select and grouped_collection_select to be sure you put the inputs in the correct order.

You can also use wrapper_html to input custom html for the wrapper div’s that simple_form creates.

wrapper_html: { id: 'my_id'}

I hope this is useful for you, as even though its simple, it took me a while of searching to find it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: