Skip to content

Instantly share code, notes, and snippets.

@alexparker
Created April 5, 2014 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexparker/9993912 to your computer and use it in GitHub Desktop.
Save alexparker/9993912 to your computer and use it in GitHub Desktop.
Strange `undefined method model_url` error. Am I missing something with decent exposure? Is the namespace on the controller causing an issue?
class Api::PreordersController < ApplicationController
respond_to :json
expose :preorder, attributes: :preorder_params
def create
preorder.save
respond_with preorder # does not work
# respond_with preorder, location: api_preorder_url(preorder) # works
end
private
def preorder_params
params.permit(:email)
end
end
require 'spec_helper'
describe Api::PreordersController do
describe '#create' do
let(:params) { {email: 'm@m.com', format: :json} }
it 'creates a new preorder' do
expect{
post :create, params
}.to change{
Preorder.count
}.by(1)
end
end
end
namespace :api do
resources :preorders, only: [:create, :show]
end
$ spring rspec spec/controllers/api/preorders_controller_spec.rb
1) Api::PreordersController#create creates a new preorder
Failure/Error: post :create, params: params, format: :json
NoMethodError:
undefined method `preorder_url' for #<Api::PreordersController:0x007facf46198f8>
# ./app/controllers/api/preorders_controller.rb:8:in `create'
# ./spec/controllers/api/preorders_controller_spec.rb:9:in `block (4 levels) in <top (required)>'
# ./spec/controllers/api/preorders_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
# -e:1:in `<main>'
@pedromamede
Copy link

"post :create, params" should be "post :create, params, format: :json" since your action respond_to json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment