1

I'm developing an API with rails 3.2 and rabl.

Basically I have a model "Asset", and a very simple associated controller:

class AssetsController < ApplicationController
  respond_to :json

  # GET /assets.json
  def index
    @assets = Asset.all
  end

  # GET /assets/1.json
  def show
    @asset = Asset.find(params[:id])
  end

  # GET /assets/1/edit
  def edit
    @asset = Asset.find(params[:id])
  end

  # POST /assets.json
  def create
    @asset = Asset.new(params[:asset])
    @asset.save
  end
end

For each action, I have a associated ACTION.json.rabl view.

For instance, my index.json.rabl is:

object @assets
attributes :kind, :description

When I issue the following commands, the Asset object is created but with null values:

curl -XPOST -d 'kind=house' 'http://localhost:3000/assets.json'

Also, where is the mapping between POST/assets.json and "create" function specified ?

Luc
  • 16,604
  • 34
  • 121
  • 183
  • The mapping is specified in the routes.rb when you do `resources :assets`. Can you post your rabl view template? – Jack Chu Mar 07 '12 at 08:16
  • @jack-chu thanks, I have updated the question with an example of the rabl file I use. Regarding the mapping, how can I do to map POST/assets.json to a method named "new" for instance ? – Luc Mar 07 '12 at 12:00

1 Answers1

1

It's normal because you do wrong on your curl call. You pass in args only kind not asset[kind] like you want in your create methode with :

@asset = Asset.new(params[:asset])

Update you curl methode with :

curl -XPOST -d 'asset[kind]=house' 'http://localhost:3000/assets.json'
shingara
  • 46,608
  • 11
  • 99
  • 105
  • just to understand the access to params[:asset], shouldn't the following command work ? curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '"asset":{"kind":"flat"}' 'http://localhost:3000/assets.json'. The params[:asset] is left empty. – Luc Mar 09 '12 at 06:35
  • I am not a curl expert :( See on your application log to see if the params get are as you want. – shingara Mar 09 '12 at 08:33