views:

20

answers:

1

Hi, What I use rails form helpers to build a form, input elements in that form have id attributes so they can match with their labels. The problem is this id is the something like person_first_name and not person_first_name_#{person.id} meaning if I have more than one form of the same type on the same page unexpected things can happen.

A perfect example of this is using jquery-ui datepicker. I have a series of forms all containing a text_field element wrapped in a div with the class datepicker. I apply the datepicker like this (in document ready) $('.datepicker input').datepicker(options) and guess what, every one of these elements, although has a seemingly working datepicker (click on input, datepicker appears), although when a date is selected in any of these datepickers only the first element on the page (of that element type, ex. input id=published_on) gets updated with the value.

Any suggestions on getting rails to output more unique element id's or make datepicker not use the id attribute?

A: 

You can easily customize the id of your input.

text_field(:person, :first_name, :id => "person_first_name_#{person.id}")
Yannis
Thanks, this worked. I would like a cleaner solution though as this breaks the label functionality (rails doesn't detect the inputs new id, so the label's :for attr is still "person_first_name" and not "person_first_name_#{person.id}").
jblaster
You can do: label(:person, : first_name, "First Name", :value => person.id) # => <label for="person_first_name_4">First name</label>
Yannis