在我的API中,我实现了以下用JSON显示用户帐户的方式(非常简单)。
class API::V1::UsersController < ApplicationController
respond_to :json
def show
respond_with User.find(params[:id])
end
end
这是我的routes.rb
Rails.application.routes.draw do
devise_for :users
# Api definition
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :users, :only => [:show]
end
end
end
截至目前,这可以让我浏览到网址:http://localhost/api/v1/users/1以显示ID为1的用户帐户。我想要的是能够输入http://localhost/api/v1/users/show?id=1以允许可以为show方法指定一个以上的参数。
我已经设置了一个rails应用程序,它希望过去以这种方式指定参数,但这次它不起作用。我假设它与我在routes.rb中定义路由的方式有关(我第一次使用资源符号)。任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
想出来。
routes.rb中:
Rails.application.routes.draw do
devise_for :users
# Api definition
namespace :api, defaults: { format: :json } do
namespace :v1 do
#resources :users, :only => [:show]
get '/users/show/' => 'users#show'
end
end
end