Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add html id to rails form_tag

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.

like image 623
Mark Avatar asked Mar 06 '09 14:03

Mark


2 Answers

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

like image 24
Kirill Warp Avatar answered Sep 24 '22 01:09

Kirill Warp


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 -%> 
like image 117
vladr Avatar answered Sep 25 '22 01:09

vladr