Ruby-on-rails 将 Rails link_to 用于发布的链接

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

Using Rails link_to for links that post

ruby-on-railsruby-on-rails-3postruby-on-rails-3.2link-to

提问by at.

I have a link that I need to submit a post request with. Normally, I'd use jQuery and prevent the link's default behavior and then submit a form to the destination. This seems like something Rails should be able to help me out with. Sure enough, the link_tomethod has an option for specifying a POST http method:

我有一个链接,我需要用它来提交发布请求。通常,我会使用 jQuery 并阻止链接的默认行为,然后将表单提交到目标。这似乎是 Rails 应该能够帮助我解决的问题。果然,该link_to方法有一个用于指定 POST http 方法的选项:

link_to "Profile", 'http://example.com/profile', method: :post

That works, but I need to add 2 parameters too. I tried:

那行得通,但我也需要添加 2 个参数。我试过:

link_to "Profile", 'http://example.com/profile', method: post, param1: 'value1', param2: 'value2'

That just added those parameters to the <a>HTML element, but didn't submit those when clicking the link:

那只是将这些参数添加到<a>HTML 元素中,但在单击链接时并未提交这些参数:

<a rel="nofollow" param1="value1" param2="value2" data-method="post" href="http://example.com/profile">Profile</a>

Is there a way to do a POST request with parameters using link_toor any other Rails method? I'm using Rails 3.2.9.

有没有办法使用参数link_to或任何其他 Rails 方法执行 POST 请求?我正在使用 Rails 3.2.9。

回答by Chris Salzberg

The short answer is that if what you mean by "parameters" is form fields, then you simply can't do this (at least not in a straightforward way that I can see). You should instead use a form with a submit button, styled to look like a link (if that's what you want it to look like).

简短的回答是,如果您所说的“参数”是表单字段,那么您根本无法做到这一点(至少不是我可以看到的直接方式)。您应该改为使用带有提交按钮的表单,其样式看起来像一个链接(如果这是您想要的样子)。

If on the other hand you had meant queryparameters, then this would work:

另一方面,如果您的意思是查询参数,那么这将起作用:

link_to "Profile", profile_path(@profile.id, param1: 'value1', param2: 'value2'), method: :post

回答by Adrien Coquio

You can encode parameters in the URL this way :

您可以通过以下方式对 URL 中的参数进行编码:

link_to "Profile", 'http://example.com/profile?' + {param1: 'value1', param2: 'value2'}.to_param, method: :post

If it does not fit your needs you are better use a form than a link_to.

如果它不符合您的需要,您最好使用表格而不是link_to.

回答by Kostas Rousis

Note that if the user has JS disabled or you have removed the unobtrusive JS libraries that come by default, link_towill be silently submitted via a GETrequest.

请注意,如果用户禁用了 JS 或您删除了默认情况下不显眼的 JS 库,link_to将通过GET请求静默提交。

In general, I am not very fond of having links that perform POSTrequests. I think that's the role of a form and a button.

一般来说,我不太喜欢有执行POST请求的链接。我认为这就是表单和按钮的作用。

Thus, an easy (and safer) alternative is to use the Rails button_tohelper:

因此,一个简单(且更安全)的替代方法是使用 Rails button_to助手:

button_to 'Profile', profile_path(@profile, param1: 'value1', param2: 'value2')

button_toalso supports the methodoption but as it defaults to postso I've just omitted it.

button_to也支持该method选项,但因为它是默认的,post所以我只是省略了它。

回答by Ammo Goettsch

In order to POST data, you need a form. However, you don't need a submit button. If you want this to look like a link for some reason, you can actually make it a link that submits the form via JavaScript. In the example below, the POST resource is just a REST action that does not require any fields so there are no form input controls. If you wanted to post some data, just put hidden input fields in the form.

为了 POST 数据,您需要一个表单。但是,您不需要提交按钮。如果您出于某种原因希望它看起来像一个链接,您实际上可以将其设置为通过 JavaScript 提交表单的链接。在下面的示例中,POST 资源只是一个不需要任何字段的 REST 操作,因此没有表单输入控件。如果您想发布一些数据,只需在表单中放置隐藏的输入字段即可。

<%= form_tag('http://something_postable', :method => :post, :class => 'internal') %></form>
<%= link_to_function('Label for Link', 'previous("form").submit()', :title => 'Hover text for link') %>

The form is assigned a class so you can style it or hide it via CSS (e.g. 'display: inline')

表单被分配了一个类,因此您可以通过 CSS 设置样式或隐藏它(例如“显示:内联”)

回答by pangpang

The parameters and the http method should be together {param1: 'value1', param2: 'value2', :method: :post}

参数和http方法要一起 {param1: 'value1', param2: 'value2', :method: :post}

<%= link_to "Profile", profile_path(@profile), {param1: 'value1', param2: 'value2', method: :post} %>