how to add html id to rails form_tag

前端 未结 4 1151
北荒
北荒 2021-01-31 13:36

I am using Rails 2.2.2 and I would like to add an id to the html form code generated by the form_tag.

<% form_tag session_path do -%>      
<% end -%&g         


        
相关标签:
4条回答
  • 2021-01-31 13:39
    <% form_tag 'session_path', :id => 'asdf' do -%>      
    <% end -%>
    

    Generates

       <form action="session_path" id="asdf" method="post"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="ed5c62c836d9ba47bb6f74b60e70280924f49b06" /></div>
       </form>
    
    0 讨论(0)
  • 2021-01-31 13:45

    This code

    form_tag(:controller => "people", :action => "search", :method => "get", :class => "nifty_form")
    

    give as result:

    <form accept-charset="UTF-8" action="/people/search?method=get&class=nifty_form" method="post">
    

    But, if you use this structure

    form_tag({:controller => "people", :action => "search"}, :method => "get", :class => "nifty_form")
    

    you will get as result

    <form accept-charset="UTF-8" action="/people/search" method="get" class="nifty_form">
    

    and it's exactly what you want

    0 讨论(0)
  • 2021-01-31 13:45

    In my case I needed to put HTML options in {} to actually add id to form_tag, like this:

    <%= form_tag(some_update_path(@some_record), { method: :put, id: "some-id" }) do <% end %>

    0 讨论(0)
  • 2021-01-31 14:01

    For <element>_tag you specify HTML attributes directly, as follows:

    <% form_tag session_path, :id => 'elm_id', :class => 'elm_class',
                              :style => 'elm_style' do -%>
       ...
    <% end -%>
    

    It is for <element>_remote_tag constructs that you need to move the HTML attributes inside a :html map:

    <% form_tag form_remote_tag :url => session_path, :html => {
                                :id => 'elm_id', :class => 'elm_class',
                                :style => 'elm_style' } do -%>
       ...
    <% end -%>
    
    0 讨论(0)
提交回复
热议问题