2015-08-25 67 views
0

我正在請求來自不同Rails應用程序的任何用戶。我正在使用grape寶石。以下代碼是請求的用戶信息發送控制器操作。Rails應用程序路由無法從Grape API訪問

,但遺憾的是,示值誤差:

NoMethodError (undefined method `profile_url' for #<Grape::Endpoint:0xdadef8c>): 

問題是什麼?顯示網址的我的rake routes確實存在。

module V1 
    class User < Grape::API 
    version 'v1', using: :path 
    format :json 
    prefix :api 

    resource :user do 

     desc "find any user" 

     params do 
     requires :email, type: String 
     requires :password, type: String 
     end 

     get :create do 
     user = User.find_by_email(params[:email]) 
     if user.present? 
      if user.valid_password?(params[:password]) 
      {user: user, profile_url: profile_url(user.profile)} 
      else 
      {:notice => "Invalid password for email: #{params[:email]}"} 
      end 
     else 
      {:notice => "No user found with email: #{params[:email]}"} 
     end 
     end 

    end 
    end 
end 

rake routes | grep profile

profiles GET  /profiles(.:format)         profiles#index 
                POST  /profiles(.:format)             profiles#create 
             new_profile GET  /profiles/new(.:format)            profiles#new 
            edit_profile GET  /profiles/:id/edit(.:format)           profiles#edit 
              profile GET  /profiles/:id(.:format)            profiles#show 
                PATCH /profiles/:id(.:format)            profiles#update 
                PUT  /profiles/:id(.:format)            profiles#update 
                DELETE /profiles/:id(.:format)            profiles#destroy 
+0

你可以分享你的'耙路線的結果| grep配置文件? – nayiaw

回答

0

確保你安裝的資源正常。

您可能需要像這樣:

mount API::v1::User 

查看更多有關mounting here

而且,看看這個post。在使用Grape寶石時,瞭解如何安裝資源可能會有幫助。

0

你可以這樣說:

Rails.application.routes.url_helpers.profile_url(user.profile)