如何将 html id 添加到 rails form_tag

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/618948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:24:17  来源:igfitidea点击:

how to add html id to rails form_tag

htmlruby-on-rails

提问by Mark

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.

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

<% 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 apiisn't really any help and I've tried adding various combinations of

API是不是真的任何帮助,我已经尝试添加的各种组合

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

with no luck.

没有运气。

回答by vladr

For <element>_tagyou specify HTML attributes directly, as follows:

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

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

It is for <element>_remote_tagconstructs that you need to move the HTML attributes inside a :htmlmap:

对于需要在地图内移动 HTML 属性的构造:<element>_remote_tag:html

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

回答by Kirill Warp

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

这正是你想要的

回答by Dutow

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

回答by Ivica Lakato?

In my case I needed to put HTML options in {}to actually add idto form_tag, like this:

就我而言,我需要将 HTML 选项放入{}实际添加idform_tag,如下所示:

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

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