39

I have this form_for:

<%= form_for [post, Comment.new,], :remote => true do |f| %>
<%= f.text_area :content, :cols =>10, :rows => 1%>
<% end %>
<%= f.submit :class => "input_comment"  %>

That generate the next code html:

<form method="post" id="new_comment" data-remote="true" class="new_comment" 
action="/post/4efcda9e1d41c82486000077/comments" accept-charset="UTF-8"><div 
style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8">
<input type="hidden" value="ctVfDF/O4FIR91I7bC5MVezQmutOCkX3dcXe73uNPZY=" name="authenticity_token">

<textarea rows="1" name="comment[content]" id="comment_content" cols="10"></textarea>
<input type="submit" value="Create Comment" name="commit" class="input_comment">
</form>

If I have many forms in a same page is not a html valid with the same id.

  • The id for form_for generate id="new_comment"
  • The id for textarea generate id="comment_content"

With so many forms in a same page is not valid html.

How can I change the id autogenerate by form_for method helper from rails 3.1?

lulalala
  • 15,996
  • 12
  • 101
  • 165
hyperrjas
  • 10,278
  • 22
  • 91
  • 188

3 Answers3

78

Adding on to what miked said, the easiest way to make unique form id's for the posts would be to use the post's id numbers in the id attribute, like so:

<%= form_for [post, Comment.new,], :remote => true, :html => { :id => "new_comment_on_#{post.id}" } do |f| %>
Batkins
  • 5,250
  • 26
  • 26
  • For text-area or button, field...etc like so `:id => "text_#{post.id}"` for example. – hyperrjas Dec 30 '11 at 19:00
  • You would do the same thing, but with a `text_area` it's unecessarry to pass it in within `:html => {}`. As in `10, :rows => 1, :id => "comment_on_#{post.id}_content" %>`. Check the docs for the [text_area form helper](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-text_area) for rails. – Batkins Dec 30 '11 at 19:00
23

I think the :namespace option is what you're looking for.

It appends the name to the form's id as well as all input and label fields.

e.g

<%= form_for [post, Comment.new,], namespace: 'NAMESPACE', :remote => true do |f| %>
    <%= f.text_area :content, :cols =>10, :rows => 1%>
<% end %>

Would generate:

Form id = NAMESPACE_new_comment

Textarea id = NAMESPACE_comment_content

From the docs:

:namespace - A namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generated HTML id

Subtletree
  • 2,980
  • 2
  • 15
  • 22
  • 4
    This is the right answer, Rails has included the namespace option especially for this use-case. – Algorini Dec 28 '15 at 10:39
  • 1
    I agree. This should be the answer. – fatuhoku Jul 18 '16 at 13:37
  • 1
    Thank you @Subtletree. The id attribute does not change by assignment in some cases. For this reason, this is the most correct approach. The version I tried: Rails: 6.0.2.2 – ozbilgic Jun 02 '20 at 10:24
15

You should be able to set the form's id to whatever you want. Something like:

<%= form_for @object, :html=> {:id => 'custom_form_id'} do |f| %>
miked
  • 4,341
  • 1
  • 15
  • 18