Emu
Emu

Reputation: 5905

Rails application route cannot access from Grape API

I'm requesting for any user from a different rails application. I'm using grape gem. The below code is the requested user's information sending controller action.

but unfortunately it is showing error:

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

What is the problem? My rake routes showing the url does exists.

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

Upvotes: 0

Views: 1209

Answers (2)

lgx
lgx

Reputation: 590

You can do it like this:

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

Upvotes: 1

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34338

Make sure you mounted the resource properly.

You may need something like this:

mount API::v1::User

See more about mounting here

Also, take a look at this post. Might be helpful to see how you can mount your resource while using Grape gem.

Upvotes: 1

Related Questions