nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support
http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/
MIT License
3.65k stars 335 forks source link

Rails-API and Rabl conflict #251

Closed krainboltgreene closed 12 years ago

krainboltgreene commented 12 years ago

Here's the relevant files:

# app/controllers/api/v1/accounts_controller.rb
class Api::V1::AccountsController < Api::V1::ApplicationController

  skip_before_filter :require_access_token, only: [:create]

  # POST /api/v1/accounts
  def create
    account = Account.new params[:account]
    if account.valid?
      render account.tap(&:save), status: :ok
    else
      render json: account.errors, status: :conflict
    end
  end

end
# config/initializers/rabl.rb
Rabl.configure do |config|
  # Commented as these are defaults
  # config.cache_all_output = false
  # config.cache_sources = false
  # config.escape_all_output = false
  # config.json_engine = nil # Any multi\_json engines
  # config.msgpack_engine = nil # Defaults to ::MessagePack
  # config.bson_engine = nil # Defaults to ::BSON
  # config.plist_engine = nil # Defaults to ::Plist::Emit
  # config.include_json_root = true
  # config.include_msgpack_root = true
  # config.include_bson_root = true
  # config.include_plist_root = true
  # config.include_xml_root  = false
  # config.enable_json_callbacks = false
  # config.xml_options = { :dasherize  => true, :skip_types => false }
  # config.view_paths = ["app/views"]
end
gem 'rails-api', '0.0.1'
gem 'rabl', '0.6.12'

And the error I get when it's time to render create:

ActionView::MissingTemplate: Missing partial api/v1/accounts/account with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :rabl]}. Searched in:
  * "/Users/krainboltgreene/Code/Ruby/manacurve-api/app/views"

    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/action_view/path_set.rb:58:in `find'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/action_view/lookup_context.rb:109:in `find'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/action_view/renderer/partial_renderer.rb:339:in `find_template'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/action_view/renderer/partial_renderer.rb:333:in `find_partial'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/action_view/renderer/partial_renderer.rb:222:in `render'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/action_view/renderer/renderer.rb:41:in `render_partial'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/action_view/renderer/renderer.rb:15:in `render'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/abstract_controller/rendering.rb:110:in `_render_template'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/abstract_controller/rendering.rb:103:in `render_to_body'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/action_controller/metal/renderers.rb:28:in `render_to_body'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/abstract_controller/rendering.rb:88:in `render'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/action_controller/metal/rendering.rb:16:in `render'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/actionpack-3.2.3/lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
    /Users/krainboltgreene/.rvm/gems/ruby-1.9.3-p194@manacurve-api/gems/activesupport-3.2.3/lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
  1. The format list doesn't have JSON
  2. It's looking for a partial?
  3. It's looking for 'api/v1/accounts/account', not 'api/v1/accounts/create'

UPDATE: I see why 2 and 3 were happening, as I was providing an object instead of the action name.

krainboltgreene commented 12 years ago

Changing to this worked:

  def create
    @account = Account.new params[:account]
    if @account.valid?
      @account.save
      render :create, status: :ok, formats: [:json]
    else
      render json: @account.errors, status: :conflict
    end
  end
databyte commented 12 years ago

Glad that worked for you. The whole formats: :json does wonders. Closing issue.