1
votes

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 ?

1
The mapping is specified in the routes.rb when you do resources :assets. Can you post your rabl view template? - Jack Chu
@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

1 Answers

1
votes

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'