Ruby-on-rails Rails 3 提交带有链接的表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4791538/
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-09-03 00:11:17  来源:igfitidea点击:

Rails 3 submit form with link

ruby-on-railsformshyperlink

提问by Marat_Galiev

How I can submit form with link on correct rails 3 format? Thanks.

如何以正确的 rails 3 格式提交带有链接的表单?谢谢。

<%= form_for @post do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<p><%= f.submit %></p>
<% end %>

My code sample.

我的代码示例。

采纳答案by polarblau

"Correct" is a tricky word in this context ;) . One could ask why you're not just taking a buttonelement and make it look like a link?

在这种情况下,“正确”是一个棘手的词;)。有人可能会问,为什么你不只是采用一个button元素,让它看起来像一个链接?

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:

无论如何? - 你不能用纯 HTML 来实现这一点(至少据我所知)。使用像 jQuery 这样的 Javascript 框架,你可以简单地做这样的事情:

$('a').click(function(){
    $('form').submit();
    return false;
});

Rails 2.3.x had a link_to_remotehelper which let's you specify a :submitparameter (= DOM element's ID, default is the parent form). So you were be able to write:

Rails 2.3.x 有一个link_to_remote帮助器,让你指定一个:submit参数(= DOM 元素的 ID,默认是父表单)。所以你可以写:

link_to_remote 'submit', :url => {…}, :submit => "my_form"

But with Rails 3's push to UJS, this helper is gone.

但是随着 Rails 3 对 UJS 的推动,这个助手消失了。

回答by Jazz

For people who came here via Google, I have an improvement on Zequez's answer. Instead of the method that he gives, add this method to the application helper instead:

对于通过 Google 来到这里的人,我对 Zequez 的回答有所改进。代替他提供的方法,将此方法添加到应用程序助手中:

def link_to_submit(*args, &block)
  link_to_function (block_given? ? capture(&block) : args[0]), "$(this).closest('form').submit()", args.extract_options!
end

Then, as Zequez stated, for simple links you can just do this in your view:

然后,正如 Zequez 所说,对于简单的链接,您可以在您的视图中执行此操作:

<%= link_to_submit 'Submit Form' %>

...and for more complicated buttons you can pass HTML options and a block to be used inside the link. If you use Twitter Bootstrap, for example, this lets you add CSS classes, formatting and icons:

...对于更复杂的按钮,您可以传递 HTML 选项和要在链接内使用的块。例如,如果您使用 Twitter Bootstrap,这可以让您添加 CSS 类、格式和图标:

<%= link_to_submit( class: 'btn btn-primary' ) do %>
  <strong>Submit</strong> the Form <i class="icon-arrow-right"></i>
<% end %>

The JQuery code will work as long as the link is a child of the form (that is, as long as link_to_submitis called from somewhere within the form_forblock).

只要链接是表单的子级(也就是说,只要link_to_submitform_for块内的某个地方调用),JQuery 代码就会工作。

回答by Zequez

You can add the following to the application helper:

您可以将以下内容添加到应用程序助手中:

def link_to_submit(text)
  link_to_function text, "$(this).closest('form').submit()"
end

Then inside your view files you can just call

然后在您的视图文件中,您可以调用

link_to_submit 'Submit Form'

And the link must be child of the form.

并且链接必须是表单的子级。

回答by nurettin

With jquery, this one-liner will work fine for a simple form.

使用 jquery,这个单行代码适用于简单的表单。

<%= link_to t("translate.submit"), "#", class: "make it beautiful", :onclick=>"$('form').submit()" %>

Of course you don't really have to use jquery, just finding the dom element for your form will work fine as well.

当然,您实际上不必使用 jquery,只需为您的表单查找 dom 元素也可以正常工作。

<%= link_to t("translate.submit"), "#", class: "make it beautiful", :onclick=>"document.getElementById('your_form_id').submit()" %>

This way you don't use any ajax, just plain form submit.

这样你就不用任何ajax,只是简单的表单提交。

回答by nessur

In Rails 3, the link_to_remotehelper is gone, but it's replaced with

在 Rails 3 中,link_to_remotehelper 不见了,但取而代之的是

link_to 'submit me', url_for(@post), {:remote => true, :class => 'submit_me'}

link_to 'submit me', url_for(@post), {:remote => true, :class => 'submit_me'}

In your case, you likely want your form to do the AJAX, like so:

在您的情况下,您可能希望您的表单执行 AJAX,如下所示:

<%= form_for @post, :remote => true do |f| %>
  <%= f.label :title %><br>
  <%= f.text_field :title %>
  <p><%= f.submit %></p>
<% end %>

With a companion link:

带有配套链接:

link_to 'submit me', '#', :class => 'submit_me'

Then, in an .js file included in the page body:

然后,在包含在页面正文中的 .js 文件中:

$('.submit_me').click(function() {
  $('form').submit();
  return false;
});

The idea is that anything more complicated than turning a link or form into an ajax request should be done with the jQuery callbacks, as listed here:

这个想法是任何比将链接或表单转换为 ajax 请求更复杂的事情都应该使用 jQuery 回调来完成,如下所示:

https://github.com/rails/jquery-ujs/wiki/ajax

https://github.com/rails/jquery-ujs/wiki/ajax

And if you want to really get into interactive AJAX requests, go here for a great 2-part article on it.

而且,如果您想真正了解交互式 AJAX 请求,请到这里阅读一篇由两部分组成的精彩文章。