views:

4200

answers:

2

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 -%>

Currently produces:

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

Would like it to produce:

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

The api isn't really any help and I've tried adding various combinations of

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

with no luck.

+8  A: 

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 -%>

Cheers, V.

vladr
This also works for: form_tag path, :name='myname'
Chris
+2  A: 
<% 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>
Dutow