简体   繁体   English

Ruby on Rails:如何使用form_tag指定html ID

[英]Ruby on Rails: how do you specify an html ID with the form_tag

I'm using the form tag, and I want the result to be nomething like this: 我正在使用form标记,并且我希望结果像这样:

<form action="controller/action" id="myID">

I currently have this 我现在有这个

<% form_tag :action => '', :id=>"relative_date_filter" do |f| %>

which generates this: 生成此:

<form action="/controller/relative_date_filter" method="post">

EDIT: 编辑:
I currently get this error: 我目前收到此错误:

compile error
/app/views/controller/filters/_relative_time.html.erb:1: syntax error, unexpected tASSOC, expecting '}'
...e=true ;  form_tag {:action => ''}, {:id => "relative_date_f...
                              ^
/app/views/controller/filters/_relative_time.html.erb:1: syntax error, unexpected ',', expecting kEND
...e ;  form_tag {:action => ''}, {:id => "relative_date_filter...
                              ^
/app/views/controller/filters/_relative_time.html.erb:1: syntax error, unexpected kDO, expecting kEND
... => "relative_date_filter"} do ; @output_buffer.concat "\n  ...
                              ^
/app/views/controller/filters/_relative_time.html.erb:14: syntax error, unexpected kENSURE, expecting $end
Extracted source (around line #1):

1: <% form_tag {:action => ''}, {:id => "relative_date_filter"} do %>

Here, what is weird is that there is no line 14. its the end of the file. 在这里,很奇怪的是,在文件末尾没有第14行。 I added a return, and it changed to line 15. My previous code did cause the syntax error. 我添加了一个返回值,并将其更改为第15行。我之前的代码确实导致了语法错误。

这应该工作:

<% form_tag({:action => ''}, {:id => "relative_date_filter"}) do |f| %>

The first parameter of form_tag is the url which is why you're seeing 'relative_date_filter' used for that. form_tag的第一个参数是url,这就是为什么您看到为此使用“ relative_date_filter”的原因。 I think the extra options may allow you to specify an id. 我认为其他选项可以允许您指定ID。

<% form_tag({:action => ''}, {:id => 'relative_date_filter'}) do |f| %>

The first hash being the url and the second any extra options. 第一个哈希是url,第二个是任何其他选项。 This is also where the form method is supplied if you want anything other than post, for example. 例如,如果您想要发布以外的任何内容,也可以在这里提供form方法。

<%= form_tag ({:controller => 'public', :action => 'search'}), ({:class => "round_corners_8"}) do %>

这个在Rails 3.0中为我工作

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

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