Topic: REST Create API

Can someone get me started with the correct approach to setting up a RESTful create API for an existing create method in the controller (I already have the index/show JSON responses working as expected)? Let's say I want to use JSON ... I am not sure what kind of request or how to send the request. Here is what I have so far ...

  def create
    @car = Car.new params[:car]

    respond_to do |format|
      if @car.save
        format.html { redirect_to @car, :notice => "Successfully created car." }
        format.json
      else
        format.html { render :action => 'new' }
        format.json
      end          
    end
  end

I see from various research that some would create a POST request using CURL but in other cases it appears some are passing in the various attributes through the HTTP call. What is the correct approach? The intent is for the create API to be called from a mobile application or other tool.

curl -X POST -d "{"car": {"make":"Chevy","model":"Avalanche"}}" -H "Content-Type: application/json" http://localhost:3000/cars.json

OR

Do I setup up a new controller method named something like 'add' and pass in the params via the URL and then create the model in the controller method? This doesn't seem to be correct or right ...

http://localhost:3000/cars/add.json?make=Chevy&model=Avalanche

Any insight/experience is greatly appreciated!

Last edited by jmesserer (2011-09-10 00:59:27)