在 rails 中使用 link_to 提交表单 [英] Submit form using link_to in rails

查看:36
本文介绍了在 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()" %>

这篇关于在 rails 中使用 link_to 提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆