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. I was using a form:
我需要一种在Rails 3中创建链接的方法,该链接将发送带有多个参数的post请求,而不会导致重新加载。我正在使用一个表格:
= 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等但似乎无法让参数正常工作。
2 个解决方案
#1
2
I would use a link with params like this:
我会使用这样的params链接:
= 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. If it doesn't work, I would assume the jquery $.ajax object isn't setting the type from the method. You might be able to override this or create your own binding to the click event.
我已经在没有远程的情况下使用了这个:true但是我没有理由认为它不适用于jquery rails。如果它不起作用,我会假设jquery $ .ajax对象没有设置方法的类型。您可以覆盖它或创建自己的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
#2
1
You can either use javascript to make a link do a form post when clicked (messy, but similar to how scaffolding does deletes) or use css to style a button to look like a link.
你可以使用javascript来点击链接做一个表格帖子(凌乱,但类似于脚手架删除的方式)或使用css来设置一个按钮看起来像一个链接。
#1
2
I would use a link with params like this:
我会使用这样的params链接:
= 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. If it doesn't work, I would assume the jquery $.ajax object isn't setting the type from the method. You might be able to override this or create your own binding to the click event.
我已经在没有远程的情况下使用了这个:true但是我没有理由认为它不适用于jquery rails。如果它不起作用,我会假设jquery $ .ajax对象没有设置方法的类型。您可以覆盖它或创建自己的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
#2
1
You can either use javascript to make a link do a form post when clicked (messy, but similar to how scaffolding does deletes) or use css to style a button to look like a link.
你可以使用javascript来点击链接做一个表格帖子(凌乱,但类似于脚手架删除的方式)或使用css来设置一个按钮看起来像一个链接。