简体   繁体   English

如果是POST,为什么需要在response_with中指定:location => some_url(@object)

[英]Why do I need to specify :location=>some_url(@object) in respond_with in the case of POST

I am implementing a RESTful API using RoR. 我正在使用RoR实现RESTful API。 Here my UsersController : 这是我的UsersController

class ApplicationController < ActionController::API
    [...]
end

module Api
  module V1
    class UsersController < ApplicationController

      respond_to :json

      def create
        @user = User.new(user_params)
        @user.save
        respond_with @user, :location => api_v1_user_url(@user)
      end

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

    end
  end
end

If I omit the @user param from :location => api_v1_user_url(@user) in UsersController#create ie I have it like so: 如果我从UsersController#create :location => api_v1_user_url(@user)中省略@user参数,那么我就这样:

respond_with @user, :location => api_v1_user_url

I get the following error: 我收到以下错误:

ActionController::UrlGenerationError in Api::V1::UsersController#create
No route matches {:action=>"show", :controller=>"api/v1/users", :format=>"json"} missing required keys: [:id]

I just added the @user param to api_v1_user_url on a hunch after I saw this error and it works. 看到此错误后,我api_v1_user_url直觉将@user参数添加到api_v1_user_url ,并且可以正常工作。

My question is what is going on here? 我的问题是这是怎么回事? I do not understand why I need to pass @user when most of the examples I've seen do not involve passing the object @user to the :location attribute. 我不明白为什么当我看到的大多数示例都不涉及将@user对象@user:location属性时,为什么需要传递@user

The output of rake routes is a follows: rake routes的输出如下:

$ rake routes
Ignoring bcrypt-3.1.7 because its extensions are not built.  Try: gem pristine bcrypt-3.1.7
          Prefix Verb   URI Pattern                      Controller#Action
    api_v1_users GET    /api/v1/users(.:format)          api/v1/users#index {:format=>"json"}
                 POST   /api/v1/users(.:format)          api/v1/users#create {:format=>"json"}
 new_api_v1_user GET    /api/v1/users/new(.:format)      api/v1/users#new {:format=>"json"}
edit_api_v1_user GET    /api/v1/users/:id/edit(.:format) api/v1/users#edit {:format=>"json"}
     api_v1_user GET    /api/v1/users/:id(.:format)      api/v1/users#show {:format=>"json"}
                 PATCH  /api/v1/users/:id(.:format)      api/v1/users#update {:format=>"json"}
                 PUT    /api/v1/users/:id(.:format)      api/v1/users#update {:format=>"json"}
                 DELETE /api/v1/users/:id(.:format)      api/v1/users#destroy {:format=>"json"}
    static_index GET    /static/index(.:format)          static#index
            root GET    /                                static#index

Another place I've seen this (just to name another one): 我见过的另一个地方(仅举一个例子):

The reason being is that api_v1_user_url or api_v1_user_path are for a single instance and you need to pass the id of that user it should look for and render. 原因是api_v1_user_urlapi_v1_user_path用于单个实例,并且您需要传递应该查找并呈现的该用户的id Hence why you got the error you go. 因此,为什么会出错?

In the examples you probably saw api_v1_users_url or api_v1_users_path which renders all the users. 在示例中,您可能会看到api_v1_users_urlapi_v1_users_path呈现了所有用户。 Notice the users_ vs user_ 注意users_ vs user_

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM