bploetz / versionist

A plugin for versioning Rails based RESTful APIs.
MIT License
972 stars 51 forks source link

Api endpoint is not working #71

Closed scrubpayjay closed 9 years ago

scrubpayjay commented 9 years ago

I am not sure if my problem is an issue or not. I have cloned a repository from github.Which is basically an api project built with versionist gem. Then i have successfully installed the project. But when i are to access my project from front-end view i got bellow routing error:

    Started GET "/user" for 127.0.0.1 at 2015-08-03 12:57:34 +0600

ActionController::RoutingError (No route matches [GET] "/user"):
  actionpack (4.1.7) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.7) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.7) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.7) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.7) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.7) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.7) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.7) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.7) lib/action_dispatch/middleware/static.rb:84:in `call'
  rack-cors (0.2.9) lib/rack/cors.rb:54:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.7) lib/rails/engine.rb:514:in `call'
  railties (4.1.7) lib/rails/application.rb:144:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
  thin (1.5.1) lib/thin/connection.rb:79:in `catch'
  thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
  thin (1.5.1) lib/thin/connection.rb:54:in `process'
  thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
  eventmachine (1.0.3) lib/eventmachine.rb:187:in `run_machine'
  eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
  thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
  thin (1.5.1) lib/thin/server.rb:159:in `start'
  rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
  rack (1.5.2) lib/rack/server.rb:264:in `start'
  railties (4.1.7) lib/rails/commands/server.rb:69:in `start'
  railties (4.1.7) lib/rails/commands/commands_tasks.rb:81:in `block in server'
  railties (4.1.7) lib/rails/commands/commands_tasks.rb:76:in `tap'
  railties (4.1.7) lib/rails/commands/commands_tasks.rb:76:in `server'
  railties (4.1.7) lib/rails/commands/commands_tasks.rb:40:in `run_command!'
  railties (4.1.7) lib/rails/commands.rb:17:in `<top (required)>'
  bin/rails:8:in `require'
  bin/rails:8:in `<top (required)>'
  spring (1.1.3) lib/spring/client/rails.rb:27:in `load'
  spring (1.1.3) lib/spring/client/rails.rb:27:in `call'
  spring (1.1.3) lib/spring/client/command.rb:7:in `call'
  spring (1.1.3) lib/spring/client.rb:26:in `run'
  spring (1.1.3) bin/spring:48:in `<top (required)>'
  spring (1.1.3) lib/spring/binstub.rb:11:in `load'
  spring (1.1.3) lib/spring/binstub.rb:11:in `<top (required)>'
  bin/spring:16:in `require'
  bin/spring:16:in `<top (required)>'
  bin/rails:3:in `load'
  bin/rails:3:in `<main>'

I have no idea why this is happening.Because my routes file has the url.Here is my rake routes output:

    GET    /user(.:format)                                     api/v1/users#show {:format=>:json}
                       PATCH  /user(.:format)                                     api/v1/users#update {:format=>:json}
                       PUT    /user(.:format)                                     api/v1/users#update {:format=>:json}
        validate_users POST   /users/validate(.:format)                           api/v1/users#validate {:format=>:json}
                       POST   /users(.:format)                                    api/v1/users#create {:format=>:json}

In my Gemfile

source 'https://rubygems.org'

ruby '2.1.4'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.7'

# Use pg as the database for Active Record
gem 'pg'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
#gem 'coffee-rails', '~> 4.0.0'

# Use jquery as the JavaScript library
#gem 'jquery-rails'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'

# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development

# Use unicorn as the app server
gem 'unicorn'
gem 'dotenv'
gem 'dotenv-rails'

group :test do
  gem 'database_cleaner'
  gem 'elasticsearch-extensions'
  gem 'shoulda-matchers'
  gem 'simplecov', require: false
end

group :development do
  gem 'foreman'
  gem 'mailcatcher'
end

group :test, :development do
  gem 'factory_girl_rails'
  gem 'rspec-rails'
  gem 'stripe-ruby-mock'
end

group :production do
  gem 'newrelic_rpm'
  gem 'rails_12factor'
end

gem 'daemons',                       '~> 1.1.9'
gem 'delayed_job_active_record',     '~> 4.0.2'
gem 'doorkeeper',                    '~> 1.4.1'
gem 'elasticsearch-model',           '~> 0.1.0'
gem 'elasticsearch-rails',           '~> 0.1.0'
gem 'errawr-rails',                  '~> 1.0.0'
gem 'haml',                          '~> 4.0.5'
gem 'kaminari',                      '~> 0.16.0'
gem 'money-rails',                   '~> 0.12.0'
gem 'phony_rails',                   '~> 0.7.0'
gem 'rack-cors',                     '~> 0.2.0'
gem 'rfc-822',                       '~> 0.4.0'
gem 'roadie',                        '~> 2.4'
gem 'rosebud',                       '~> 0.1.0'
gem 'soulless',                      '~> 0.5.0'
gem 'sorcery',                       '~> 0.8.6'
gem 'squeel',                        '~> 1.1'
gem 'stripe',                        '~> 1.16'
gem 'versionist',                    '~> 1.4.0'
gem 'workless',                      '~> 1.2.3'

my routes file

Rails.application.routes.draw do
use_doorkeeper do
   skip_controllers :applications, :authorizations, :authorized_applications, :token_info
end

api_version(module: 'api/v1', header: { name: 'Accept', value: 'application/vnd.scrubpay.com;       version=1' }, defaults: { format: :json }) do
  resources :password_resets, only: [:create, :update]
  resource :user, only: [:show, :update]

  resources :users, only: [:create] do
    post :validate, on: :collection
   end
 end

Could you please help me why this routing error is happening ? When i change my routes file to namespace instead of versionist like bellow

namespace: :api do
  namespace: :v1 do 
  #respetive resource goes here  
  end
end

Then its working fine. But its not working when i am using api_version() method .can any one tell me what is the reason behind this problem? thank you.

scrubpayjay commented 9 years ago

i believe this error is happening for accept header hash. That is header: { name: 'Accept', value: 'application/vnd.scrubpay.com; version=1' } this line of code is the problem i guess.

bploetz commented 9 years ago

You've configured the API version to use the Accept header to specify the requested version. Are you sending the Accept header in your request and setting it to the configured value?

scrubpayjay commented 9 years ago

Thank you very much for your valuable feedback. I already specified the Accept header like below:

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Authorization:Bearer 5620eb92ed68928e446cd4f204b871c3d6c178b4c00199ed8751cddbf74ceb5a
Connection:keep-alive
Content-Type:application/json
Host:localhost:3000
If-None-Match:"84242edbe8f46112871a1420093e976b"
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu   Chromium/41.0.2272.76 Chrome/41.0.2272.76 Safari/537.36

Any suggestion did i missed something in the Accept header option

bploetz commented 9 years ago

I don't see the version you configured in your routes ('application/vnd.scrubpay.com; version=1') in the Accept header above....

scrubpayjay commented 9 years ago

can you please give some idea how to include version in my ajax request uri.

scrubpayjay commented 9 years ago

@bploetz i am really great-full to you for pointing me out that i was missing Accept header.And now its fixed. Thank you very much

bploetz commented 9 years ago

:+1: