Creating unique HTML identifiers in Rails using re-partial, which has form_for

I have an idea about my current project, which is doing something like the following (in haml):

-@horses.each do |horse|
  = render :partial => 'main/votingbox', :locals => {:horse => horse}

In the _votingbox.html.haml file, I have the following:

%div.votingbox
  %span.name= horse.name
  %div.genders
    - if horse.male
      %img{:src => 'images/male.png', :alt => 'Male'} 
    - if horse.female
      %img{:src => 'images/female.png', :alt => 'Female'}
  %div.voting_form
    = form_for(Vote.new, {:url => horse_vote_path(horse)}) do |f|
      = f.label :comment, "Your opinion"
      = f.text_field :comment
      ...
      a bunch of labels and input elements follow generated using the form helpers

This will generate working code, but it generates forms with the same identifiers for all form elements, which makes HTML invalid when the partial ballot is displayed a second time.

My first suggestion for fixing this issue was to specify a unique: id for form_for, but this only applies to the form tag generated by form_for, and not to any of the tags inside the form_for block.

- form_for , . , .

, Rails ?

+5
2

, .

: , , ActiveRecord, form_for, : , form_for. . fields_for .

= form_for(Vote.new, :url => horse_vote_path(horse), :id => dom_id(horse, 'vote')) do |f|
  = f.fields_for horse, :index => horse do |fh|
    = fh.text_field :whatever
+1

id name form_for :as :

= form_for(Vote.new, :as => horse.name, {:url => horse_vote_path(horse)}) do |f|
  = f.label :comment, "Your opinion"
  = f.text_field :comment

, horse.name foobar, , id foobar_comment, foobar[comment]

, html-, horse.name hor$e html- , , - .

P.S: , , , ! , - -!

+1

All Articles