Rails 3 提交带有链接的表单 [英] Rails 3 submit form with link
问题描述
如何以正确的 rails 3 格式提交带有链接的表单?谢谢.
How I can submit form with link on correct rails 3 format? Thanks.
<%= form_for @post do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<p><%= f.submit %></p>
<% end %>
我的代码示例.
推荐答案
正确"在这种情况下是一个棘手的词 ;) .有人可能会问,为什么不只是采用 button
元素并使其看起来像一个链接?
"Correct" is a tricky word in this context ;) . One could ask why you're not just taking a button
element and make it look like a link?
无论如何 - 您无法使用纯 HTML 实现这一点(至少据我所知).使用 Javascript 框架,例如jQuery 你可以简单地做这样的事情:
Anyways — you can't achieve this with plain HTML (at least not to my knowledge). With a Javascript framework like e.g. jQuery you could simply do something like this:
$('a').click(function(){
$('form').submit();
return false;
});
Rails 2.3.x 有一个 link_to_remote
帮助器,让您指定 :submit
参数(= DOM 元素的 ID,默认为父表单).所以你可以写:
Rails 2.3.x had a link_to_remote
helper which let's you specify a :submit
parameter (= DOM element's ID, default is the parent form). So you were be able to write:
link_to_remote 'submit', :url => {…}, :submit => "my_form"
但是随着 Rails 3 向 UJS 的推动,这个助手消失了.
But with Rails 3's push to UJS, this helper is gone.
这篇关于Rails 3 提交带有链接的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!