How to get a POST data from in controller?

Hi, I am newbie in Ruby. Here is the situation I meet: I got a javascript app that will send string data by using HTTP POST method to another ruby app. Here is the url: http://localhost:3000/home/receive_data So, the ruby app got a home controller and receive_data action to handle it. It will write log when receive_data action works just like the following code does:

  def receive_data     trace "receive_data..."     render :text => "ok"   end

But I never see logs appear when the javascript app send data. If I change the HTTP method of javascript app while sending data to GET, then it will get logs. It means the action in controller only can handle a request of GET method? How should I do, if I wanna receive data from POST method?

firestoke

Hi,

normally Rails takes care of this automagically, and it doesn't matter to an action whether the data comes in as GET or POST - in both cases, the data is accessible through the params method.

If you're not getting anything in the logs, I would try to locate the problem rather within the calling javascript than within the rails app. If all else fails, have you tried POSTing to the action manually, via wget or with a hand-built HTML form?

Out of sheer interest, what's 'trace'? Sorry if this is an obvious question, but I am slightly hungover at the moment .. :slight_smile:

Jan

Sorry! I forget to explain the trace function. In fact, it works just like "puts" with a additional timestamp log at head. Here it is:

  def trace(str = '')     p "["+Time.new.to_s(:db)+"] ***** "+str.to_s   end

And I forget to say, it is a AJAX POST request from the javascript app. When I test it again today, it shows up the error like below (I didn't alert it before): ActionController::InvalidAuthenticityToken in HomeController#receive_data

After known what the error it is, I already found the solution in this article: http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-cross-site-request-forging-prevention I override the verifiable_request_format? method in controller, then the receive_data action works just as I expected. Thank for yor help any way! :slight_smile:

firestoke