简体   繁体   English

Ruby on Rails链接与发布请求

[英]Ruby on Rails link with post request

I need a way to create a link in Rails 3 that will send a post request with several parameters and not cause a reload. 我需要一种在Rails 3中创建链接的方法,该链接将发送带有多个参数的发布请求,并且不会导致重新加载。 I was using a form: 我正在使用一种形式:

= form_for Vote.new, :remote => true do |f|
  .actions
    = f.submit 'Vote'
  = hidden_field_tag('question', q.id)
  = hidden_field_tag('seat', @seat.id)

but it slowed my render time down quite a bit and I don't want a submit button. 但是它大大降低了我的渲染时间,并且我不需要提交按钮。 Is there any other way to submit a form like that without using a button? 还有其他不用按钮就可以提交类似表格的方法吗?

I tried =link_to and such but couldn't seem to get the parameters working correctly. 我尝试了= link_to之类的方法,但似乎无法使参数正常工作。

I would use a link with params like this: 我将使用带有以下参数的链接:

= link_to "Post", new_vote_path(vote: {question_id: question.id, seat_id: seat.id}, remote: true, method: :post, rel: "nofollow"

I've used this without remote: true but I see no reason why it shouldn't work with jquery rails. 我在没有remote的情况下使用了它:true,但是我看不出为什么它不应该与jquery rails一起工作。 If it doesn't work, I would assume the jquery $.ajax object isn't setting the type from the method. 如果不起作用,我将假定jquery $ .ajax对象未从该方法设置类型。 You might be able to override this or create your own binding to the click event. 您可能可以覆盖此设置,也可以创建自己的click事件绑定。

Just for reference - link_to api: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to 仅供参考-link_to api: http : //api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

您可以使用javascript来使链接被单击后进行表单发布(混乱,但类似于脚手架删除的方式),也可以使用CSS设置按钮样式,使其看起来像链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM