【发布时间】:2014-05-10 14:42:47
【问题描述】:
- 列表项
我的 config/routes.rb 文件...
Rails.application.routes.draw do
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :hotels do
resources :rooms
end
end
end
我的应用程序/controllers/api/v1/hotels_controller.rb
module Api
module V1
class HotelsController < ApplicationController
respond_to :json
skip_before_filter :verify_authenticity_token
def index
@hotels = Hotel.all
respond_with ({hotels: @hotels}.as_json)
#respond_with(@hotels)
end
def show
@hotel = Hotel.find(params[:id])
respond_with (@hotel)
end
def create
@hotel = Hotel.new(user_params)
if @hotel.save
respond_with (@hotel) #LINE 21
end
end
private
def user_params
params.require(:hotel).permit(:name, :rating)
end
end
end
end
当我通过 Postman 进行 POST 时,我的数据保存得很好,但我得到了这个 NoMethodError。为什么是这样?问题似乎发生在第 21 行,即 respond_with(@hotel) 行。它不应该只是通过 show 方法响应新创建的酒店的 json 输出吗?
(1.1ms) COMMIT
Completed 500 Internal Server Error in 76ms
NoMethodError (undefined method `hotel_url' for #<Api::V1::HotelsController:0x0000010332df58>):
app/controllers/api/v1/hotels_controller.rb:21:in `create'
Rendered /Users/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered /Users/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.7ms)
Rendered /Users/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.4ms)
Rendered /Users/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (31.5ms)
【问题讨论】:
-
发布您的
rake routes输出
标签: ruby-on-rails json post