开发者

Rails 3 Form Element's id attr not unique

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_fir开发者_JAVA技巧st_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?


You can easily customize the id of your input.

text_field(:person, :first_name, :id => "person_first_name_#{person.id}")


One would think that if you set the ID of the form to say :id => "edit_person_#{person.id}" that this ID would cascade downwards into the label and field logic. From what I can see, there's too much auto-magic in the way of adding an eloquent custom solution.


# Haml
= form_for @model do |f|
  .field
    = f.label :field_name
    = f.text_field :field_name

When using the above example, ids are rendered as expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜