Change html form id generated by form_for rails 3.1

前端 未结 3 936
心在旅途
心在旅途 2021-02-06 21:41

I have this form_for:

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


        
相关标签:
3条回答
  • 2021-02-06 22:21

    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| %>
    
    0 讨论(0)
  • 2021-02-06 22:29

    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

    0 讨论(0)
  • 2021-02-06 22:35

    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| %>
    
    0 讨论(0)
提交回复
热议问题