简体   繁体   English

Rails 3:对未定义的URL帮助程序的响应有错误

[英]Rails 3: respond_with errors on undefined URL helper

I have an Artwork model that is manipulated only by API endpoints right now. 我有一个Artwork模型,现在只能由API端点操作。 (You'll see why this is important shortly). (你会明白为什么这很重要)。 Those API endpoints are declared like so in my routes.rb file: 这些API端点在我的routes.rb文件中声明如下:

namespace :api do
  namespace :v1, :defaults => { :format => :json } do
    resources :artworks, :only => [:create, :destroy, :index, :show, :update]

This results in the following routes: 这导致以下路线:

api_v1_artworks GET        /api/v1/artworks(.:format)                                             api/v1/artworks#index {:format=>:json}
                POST       /api/v1/artworks(.:format)                                             api/v1/artworks#create {:format=>:json}
api_v1_artwork  GET        /api/v1/artworks/:id(.:format)                                         api/v1/artworks#show {:format=>:json}
                PUT        /api/v1/artworks/:id(.:format)                                         api/v1/artworks#update {:format=>:json}
                DELETE     /api/v1/artworks/:id(.:format)                                         api/v1/artworks#destroy {:format=>:json}

Relevant code: 相关代码:

class Api::V1::ArtworksController < Api::V1::ApiController
  def create
    artwork = Artwork.create(artwork_params)

    respond_with artwork
  end

The Problem 问题

When #create succeeds, respond_with chokes: #create成功时, respond_with #create

`undefined method `artwork_url' for #<Api::V1::ArtworksController:0x007fea1b4c67f8>`

It's expecting the helper for the HTTP Location to be artwork_url . 它期望HTTP位置的帮助器是artwork_url How do I tell it to use api_v1_artwork_url instead? 我怎么告诉它使用api_v1_artwork_url呢? Can I alias the URL helper? 我可以为URL帮助器设置别名吗?

In this case, you'd need to specify the namespace for the responder. 在这种情况下,您需要为响应者指定名称空间。 Try: 尝试:

respond_with :api, :v1, artwork

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

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