mattwallace
mattwallace

Reputation: 4222

Specifying POST in rails 3 routes and calling from Flash

I am using rails for my backend to a Flash app. I need to send a bunch of params to rails from flash using post. I have a rails action in my users_controller called register

my routes file looks like this

  resources :users

  match 'register'  => 'Users#register', :via => :post

the actionscript looks something like this

var vars:URLVariables = new URLVariables();
vars.first_name = "First";
vars.last_name = "Last";
vars.address = "555 Mysteet Rd.";
vars.email = "[email protected]";
vars.city = "The Hill";
vars.provice = "KY";
vars.postal_code = "55555";

var request : URLRequest = new URLRequest("/register");
request.method = URLRequestMethod.POST;
request.data = vars;

var loader : URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, registerHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
loader.load(request);

The error I am getting in Flash is an IO_ERROR Streaming Event bla bla bla witch usually indicates that the URL you are calling does not exist. If I change the request.method to GET then the call works but not all the data gets sent to rails because GET can't handle the length of the data being sent.

Upvotes: 1

Views: 1438

Answers (1)

David Ortinau
David Ortinau

Reputation: 271

What does your controller and action look like?

For API type calls, such as from Flash/Flash to Rails, I don't use custom routings. Rather I rely on RESTful actions. So I'm calling:

/users/create/

Since you declared your resource for :users in your routes, you should have this routing in place (assuming you have a RESTful create action).

Gimme me details and I'll try to help more.

[edit]

More details from Matt via IM:

  • controller is Users and action is Register
  • POST is successfully received in Rails, but throws the error

    ActionController::InvalidAuthenticityToken

For security Rails 3 adds a hidden form field with a token that is expected with any POST to a Rails controller. Because this POST is occurring from Flash, this token is obviously missing and we get this error.

To fix the issue, disable the authenticity check for the controller with:

skip_before_filter :verify_authenticity_token

Upvotes: 4

Related Questions