ruby-on-rails-3controlleractionpack

how do I separate user params vs Action Pack params?


I have a Rails application where user parameters are all provided via a RESTful API with JSON parameters. Specifically, there is no client-side HTML form from which the user posts data: it's raw JSON.

So to create a new Car entry, the user might:

POST www.mysite.com/api/car
model=Ford&year=2012

In my app, by the time I receive this, the Action Pack values are intermingled with the user values in the params[] hash, so I get:

params = {:model=>"Ford", :year=>"2012", :format=>"json", :action=>"create", :controller=>"api/cars"}

What's the best way to separate the user-generated parameters from parameters generated by Action Pack? The best I can think of is to delete the latter:

car_params = params.reject {|k,v| [:format, :action, :controller].member?(k)}
car = car.new(car_params)

but that doesn't smell right. Is there a better way? (For example, can I get Action Pack to encapsulate the user supplied params into a single hash and pass that as a single element of params[]?)


Solution

  • Don't know if it can help, but I'd just create a method in application_controller :

    def user_params
      return params.reject {|k,v| [:format, :action, :controller].member?(k)}
    end
    

    So throughout the code, you can just use user_params when you don't want ActionPack params