如何将html id添加到rails form_tag

时间:2009-03-06 14:02:00

标签: html ruby-on-rails

我正在使用Rails 2.2.2,我想在form_tag生成的html表单代码中添加一个id。

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

目前生产:

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

希望它产生:

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

api并没有任何帮助,我尝试添加

的各种组合
:html => {:id => 'login_form'}
没有运气。

4 个答案:

答案 0 :(得分:98)

对于<element>_tag,您可以直接指定HTML属性,如下所示:

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

适用于在<element>_地图中移动HTML属性所需的remote _tag :html构造:

<% form_tag form_remote_tag :url => session_path, :html => {
                            :id => 'elm_id', :class => 'elm_class',
                            :style => 'elm_style' } do -%>
   ...
<% end -%>

答案 1 :(得分:27)

此代码

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

结果:

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

但是,如果你使用这个结构

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

你会得到结果

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

这正是你想要的

答案 2 :(得分:3)

<% form_tag 'session_path', :id => 'asdf' do -%>      
<% end -%>

生成

   <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 :(得分:0)

在我的情况下,我需要在{}中添加HTML选项以实际将id添加到form_tag,如下所示:

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