繁体   English   中英

如何将 html id 添加到 rails form_tag

[英]how to add html id to rails form_tag

我正在使用 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'}

没有运气。

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

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

对于<element>_ remote _tag构造,您需要在:html map 中移动 HTML 属性:

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

这段代码

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

这正是你想要的

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM