JRPete
JRPete

Reputation: 3144

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. 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.

Upvotes: 0

Views: 1180

Answers (2)

Matt Smith
Matt Smith

Reputation: 3529

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. 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.

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

Upvotes: 2

jdc
jdc

Reputation: 743

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.

Upvotes: 1

Related Questions