2016-01-05 114 views
0

我正在使用另一個rails應用程序的現有代碼創建我的第一個用於高音揚聲器克隆的完整rails API。 我想要做的第一件事是讓新用戶的創建通過API,所以我必須在用戶控制下:API/V1/users_controller.rb:即使路由正常,由於路由錯誤導致API POST失敗

class Api::V1::UsersController < Api::V1::BaseController 
    before_filter :authenticate_user!, only: [:show, :update, :destroy] 

    def index 
    if params[:follower_id] 
     users = User.find(params[:follower_id]).followers 
    elsif params[:following_id] 
     users = User.find(params[:following_id]).following 
    else 
     users = User.all.order(created_at: :asc) 
    end 

    users = apply_filters(users, params) 

    users = paginate(users) 

    users = policy_scope(users) 

    render(
     json: ActiveModel::ArraySerializer.new(
     users, 
     each_serializer: Api::V1::UserSerializer, 
     root: 'users', 
     meta: meta_attributes(users) 
    ) 
    ) 
    end 

    def show 
    user = User.find(params[:id]) 
    authorize user 

    render(json: Api::V1::UserSerializer.new(user).to_json) 
    end 

    def create 
    user = User.new(create_params) 
    return api_error(status: 422, errors: user.errors) unless user.valid? 

    user.save! 
    user.activate 

    render(
     json: Api::V1::UserSerializer.new(user).to_json, 
     status: 201, 
     location: api_v1_user_path(user.id) 
    ) 
    end 

    def update 
    user = User.find(params[:id]) 
    authorize user 

    if !user.update_attributes(update_params) 
     return api_error(status: 422, errors: user.errors) 
    end 

    render(
     json: Api::V1::UserSerializer.new(user).to_json, 
     status: 200, 
     location: api_v1_user_path(user.id), 
     serializer: Api::V1::UserSerializer 
    ) 
    end 

    def destroy 
    user = User.find(params[:id]) 
    authorize user 

    if !user.destroy 
     return api_error(status: 500) 
    end 

    head status: 204 
    end 

    def messages 
    messages = User.find(params[:id]).messages 
    render json: ActiveModel::ArraySerializer.new(messages, each_serializer: Api::V1::MessagesSerializer).to_json 
    end 

    def followed 
    followed = User.find(params[:id]).followed 
    render json: ActiveModel::ArraySerializer.new(followed, each_serializer: Api::V1::UsersSerializer).to_json 
    end 

    def followers 
    followers = User.find(params[:id]).followers 
    render json: ActiveModel::ArraySerializer.new(followers, each_serializer: Api::V1::UsersSerializer).to_json 
    end 

    def follow 
    current_user_id = User.where(api_token: request.headers['X-Api-Key']).pluck(:id) 
    Follow.new({follower_id: current_user_id, followed_id: params[:id]}).save 
    respond_with 
    end 

    def unfollow 
    current_user_id = User.where(api_token: request.headers['X-Api-Key']).pluck(:id)  
    Follow.where('followed_id = ? AND follower_id = ?', params[:id], current_user_id).destroy_all 
    end 

    private 

    def create_params 
    params.require(:user).permit(
     :email, :password, :password_confirmation, :username 
    ).delete_if{ |k,v| v.nil?} 
    end 

    def update_params 
    create_params 
    end 
end 

使用電話後後郵差:

Started POST "/api/v1/users/[email protected]&password=[FILTERED]&password_confirmation=[FILTERED]&username=fabriciofreitag" for 127.0.0.1 at 2016-01-05 13:40:02 +0100 

我得到以下錯誤:

ActionController::RoutingError (No route matches [POST] "/api/v1/users/create"): 
    actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 
    actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
    railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app' 
    railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call' 
    activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged' 
    activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged' 
    activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged' 
    railties (4.1.8) lib/rails/rack/logger.rb:20:in `call' 
    actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
    rack (1.5.5) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.5.5) lib/rack/runtime.rb:17:in `call' 
    activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call' 
    rack (1.5.5) lib/rack/lock.rb:17:in `call' 
    actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call' 
    rack (1.5.5) lib/rack/sendfile.rb:112:in `call' 
    railties (4.1.8) lib/rails/engine.rb:514:in `call' 
    railties (4.1.8) lib/rails/application.rb:144:in `call' 
    rack (1.5.5) lib/rack/lock.rb:17:in `call' 
    rack (1.5.5) lib/rack/content_length.rb:14:in `call' 
    rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service' 
    /home/fabricio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service' 
    /home/fabricio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run' 
    /home/fabricio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread' 

如果我運行 「耙路線」:

[email protected]:~/Desktop/little_tweet$ rake routes 
     Prefix Verb URI Pattern       Controller#Action 
users_sign_out GET /users/sign_out(.:format)    devise/sessions#destroy 
       POST /:controller(/:action(/:id(.:format))) :controller#:action 
       GET /:controller(/:action(/:id(.:format))) :controller#:action 
      root GET /         messages#index 
    api_v1_users GET /api/v1/users(.:format)    api/v1/users#index 
       POST /api/v1/users(.:format)    api/v1/users#create 
    api_v1_user GET /api/v1/users/:id(.:format)   api/v1/users#show 
       PATCH /api/v1/users/:id(.:format)   api/v1/users#update 
       PUT /api/v1/users/:id(.:format)   api/v1/users#update 
       DELETE /api/v1/users/:id(.:format)   api/v1/users#destroy 
api_v1_messages GET /api/v1/messages(.:format)    api/v1/messages#index 
       POST /api/v1/messages(.:format)    api/v1/messages#create 
api_v1_message GET /api/v1/messages/:id(.:format)   api/v1/messages#show 
       PATCH /api/v1/messages/:id(.:format)   api/v1/messages#update 
       PUT /api/v1/messages/:id(.:format)   api/v1/messages#update 
       DELETE /api/v1/messages/:id(.:format)   api/v1/messages#destroy 
api_v1_sessions POST /api/v1/sessions(.:format)    api/v1/sessions#create 

回答