在 rails 中使用 link_to 提交表单
问题描述:
我正在尝试使用 link_to 提交表单,如下所示:
I'm trying to submit a form using link_to as follows:
<%= form_for(@post, :url=> '/post/action', :method=> 'post', :html => {:id=>'form_id'} ) do |f| %>
....
<%= link_to 'submit', "/post/action", :onclick=>"document.getElementById('form_id').submit()" %>
....
但它不是发布表单,它只是将我的表单重定向到指定的 url.有人知道怎么做吗?
but it is not posting the form, it is simply redirecting my form to the specified url. Does anyone know how to do this?
答
您可以使用:
<%= link_to 'submit', "#", :onclick => "$('#form_id').submit()" %>
如果您使用的是 JQuery 和更高版本的 rails.
if you are using JQuery and a later version of rails.
上面可以工作,但如果你有一个很长的页面,它会因为#"而导航到页面顶部,所以如果你想避免这种情况,你可以这样做:
Above will work but if you have a really long page it will navigate to top of the page because of "#" so if you want to avoid that you can do:
<%= link_to 'submit', "", :onclick => "$('#form_id').submit()" %>