ruby on rails - Wrong number of arguments 0 for 1 unable to serialize model using devise -


i trying create api rails application in controllers folder have created following folder structure

controllers > api > v1

my routes require 'api_constraints'

myapp::application.routes.draw   devise_for :users    resources :users    ......    other resources , matching standard application   ......    # api definition   namespace :api, defaults: { format: :json },constraints: { subdomain: 'api' }, path: '/'      scope module: :v1,constraints: apiconstraints.new(version: 1, default: true)       resources :sessions, :only => [:create]       resources :users, :only => [:show]     end   end end 

i same error in both sessions , users controllers. post user controller because it's shorter

class api::v1::userscontroller < apicontroller   respond_to :json    def show     respond_with user.find(params[:id])   end end 

then tests are

require 'spec_helper'  describe api::v1::userscontroller    describe "get #show"     before(:each)       @user = factorygirl.create :user       :show, id: @user.id, format: :json     end      "returns information reporter on hash"       user_response = json.parse(response.body, symbolize_names: true)       expect(user_response[:email]).to eql @user.email     end      { should respond_with 200 }   end end 

and output test is

4) api::v1::userscontroller #show failure/error: :show, id: @user.id, format: :json argumenterror: wrong number of arguments (0 1)

two problems 1) reason id isn't getting sent api action 2) i'm not sure how api. thought should api.localhost:3000/users/1

thanks in advance help

update output rake routes

api_sessions post   /sessions(.:format)                         api/v1/sessions#create {:format=>:json, :subdomain=>"api"}  api_user    /users/:id(.:format)                        api/v1/users#show {:format=>:json, :subdomain=>"api"} 

update 2 looks duplicate wrong number of arguments (0 1) while create user

unfortunately solution post isn't option me. can't remove devise because user model shared in standard web rails application , api portion of application

update 3 looking @ other ways of having api standard app, , using devise doorkeeper seems better solution token authentication. after getting setup in same situation of

wrong number of arguments (0 1)

in server output see following output. valid user id.

started "/api/v1/users/1" ::1 @ 2015-07-27 20:52:09 +0100  processing api::v1::userscontroller#show */*    parameters: {"id"=>"1"}  geokit using domain: localhost    user load (0.4ms)  select  "users".* "users" "users"."id" = $1 limit 1  [["id", 1]]  completed 500 internal server error in 21ms  argumenterror (wrong number of arguments (0 1)):   app/controllers/api/v1/users_controller.rb:5:in `show' 

with invalid id output

started "/api/v1/users/134" ::1 @ 2015-07-27 20:55:36 +0100      processing api::v1::userscontroller#show */*    parameters: {"id"=>"134"}  geokit using domain: localhost    user load (0.5ms)  select  "users".* "users" "users"."id" = $1 limit 1  [["id", 134]]  completed 500 internal server error in 6ms  nomethoderror (undefined method `api_error' #<api::v1::userscontroller:0x007fc5a17bf098>):   app/controllers/api_controller.rb:23:in `not_found' 

update 4 after inserting debug statements id being passed through controller action , user being retrieved database.

the issue with

respond_with user.find(params[:id]) 

rails unable serialize user. have tried replacing user model not have devise enabled , can serialize model. i'm not sure why devise causing issue here.

1: verify in console factorygirl able return user object created user factory isn't nil in request.

2: run rake routes verify api routes generating like. i'm assuming if haven't set up, need edit hosts file or use pow on mac or nginx w/ dnsmasq on linux enable subdomain support in local development environment. manually test api controller whatever subdomain configured http://api.myappname.dev/api/v1/users/1.json make sure can see returing valid json response url.

a little bit cleaner example of api namespacing in routes:

namespace :api, :path => "", :constraints => {:subdomain => "api"}   namespace :v1, defaults: { format: 'json' }    ...    end end 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -