2015-08-25 44 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)