How to add html id to rails form_tag

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

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

Currently produced:

 <form action="/session" method="post"> </form> 

I would like to create:

 <form id="login_form" action="/session" method="post"> </form> 

The api doesn't really help, and I tried to add various combinations

 :html => {:id => 'login_form'} 

bad luck.

+66
html ruby-on-rails
Mar 06 '09 at 14:01
source share
3 answers

In the <element>_tag you specify the HTML attributes directly:

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

In the <element>_ remote _tag you need to move the HTML attributes inside the map :html :

 <% form_tag form_remote_tag :url => session_path, :html => { :id => 'elm_id', :class => 'elm_class', :style => 'elm_style' } do -%> ... <% end -%> 
+90
Mar 06 '09 at 14:04
source share

This code

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

enter the 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 the result

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

and that is exactly what you want

+24
Oct 27 '12 at 23:36
source share
 <% form_tag 'session_path', :id => 'asdf' do -%> <% end -%> 

Forms

  <form action="session_path" id="asdf" method="post"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="ed5c62c836d9ba47bb6f74b60e70280924f49b06" /></div> </form> 
+3
Mar 06 '09 at 14:07
source share



All Articles