【问题标题】:Changing method of parameter specification in rails urlrails url中参数指定的更改方法
【发布时间】:2016-02-14 01:41:34
【问题描述】:

在我的 API 中,我实现了以下在 JSON 中显示用户帐户的方式(非常简单)。

class API::V1::UsersController < ApplicationController
  respond_to :json

  def show
    respond_with User.find(params[:id])
  end
end

这是我的路线.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

到目前为止,这允许我浏览到 URL:http://localhost/api/v1/users/1 以显示 ID 为 1 的用户帐户。我想要的是能够键入 http://localhost/api/v1/users/show?id=1 以允许指定更多不仅仅是 show 方法的一个参数。

我已经设置了一个 Rails 应用程序,它希望过去以这种方式指定参数,但这次它不起作用。我假设这与我在 routes.rb 中定义路由的方式有关(我第一次使用资源 do 表示法)。任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: ruby-on-rails api model controller


    【解决方案1】:

    想通了。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-08
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      相关资源
      最近更新 更多