3

How do I make a remote POST request with custom parameters with a link_to helper in rails 3?

I tried something like this:

link_to 'Submit', model_path, :query => "value", :remote => true, :method => :post

The POST works and the control comes to the action in the controller, but I don't get the POST parameters in params or anywhere else.

How do I do this?

akula1001
  • 4,576
  • 12
  • 43
  • 56

2 Answers2

11

Well, I got it. I need to pass parameters to the model_path function itself like,

 model_path(:query => "value")

Didn't realize it was a function all along. Paradigm change...

akula1001
  • 4,576
  • 12
  • 43
  • 56
  • Typically however, you'll name the parameter as: model_name[parameter_name]. e.g. <%= link_to job_search.query, job_searches_path("job_search[query]" => "#{job_search.query}"), :method => :post %> – Kedar Mhaswade Sep 19 '11 at 23:26
1

You need made 2 things

  1. Call the csrf_meta_tag helper in your head of HTML
  2. Add the rails.js for your javascript library. Prototype or jQuery.
shingara
  • 46,608
  • 11
  • 99
  • 105