Ruby on rails 3 创建嵌套资源时,Rails路由错误,带有respond_with
我正在创建的一个简单的Rails 3应用程序有以下问题。我快发疯了 我的模型:Ruby on rails 3 创建嵌套资源时,Rails路由错误,带有respond_with,ruby-on-rails-3,nested-routes,respond-with,Ruby On Rails 3,Nested Routes,Respond With,我正在创建的一个简单的Rails 3应用程序有以下问题。我快发疯了 我的模型: class Vehicle < ActiveRecord::Base has_many :vehicle_pictures, :dependent => :destroy def class VehiclePicture < ActiveRecord::Base belongs_to :vehicle end 我的rake路由输出: vehicle_pictures GET
class Vehicle < ActiveRecord::Base
has_many :vehicle_pictures, :dependent => :destroy
def
class VehiclePicture < ActiveRecord::Base
belongs_to :vehicle
end
我的rake路由
输出:
vehicle_pictures GET /vehicles/:vehicle_id/pictures(.:format) {:action=>"index", :controller=>"vehicle_pictures"}
POST /vehicles/:vehicle_id/pictures(.:format) {:action=>"create", :controller=>"vehicle_pictures"}
new_vehicle_picture GET /vehicles/:vehicle_id/pictures/new(.:format) {:action=>"new", :controller=>"vehicle_pictures"
edit_vehicle_picture GET /vehicles/:vehicle_id/pictures/:id/edit(.:format) {:action=>"edit", :controller=>"vehicle_pictures"}
vehicle_picture GET /vehicles/:vehicle_id/pictures/:id(.:format) {:action=>"show", :controller=>"vehicle_pictures"}
PUT /vehicles/:vehicle_id/pictures/:id(.:format) {:action=>"update", :controller=>"vehicle_pictures"}
DELETE /vehicles/:vehicle_id/pictures/:id(.:format) {:action=>"destroy", :controller=>"vehicle_pictures"}
My vehicle_pictures_controller.rb:
class VehiclePicturesController < ApplicationController
before_filter :find_vehicle
respond_to :html, :json, :xml
private
def find_vehicle
@vehicle = Vehicle.find(params[:vehicle_id])
end
public
# GET /vehicle/:id/pictures
def index
@pictures = @vehicle.vehicle_pictures
respond_with(@pictures)
end
# GET /vehicle/:vehicle_id/pictures/new
def new
@picture = VehiclePicture.new
@picture.vehicle = @vehicle
respond_with(@picture)
end
# POST /vehicle/:vehicle_id/pictures
def create
@picture = @vehicle.vehicle_pictures.build(params[:vehicle_picture])
flash[:notice] = 'Vehicle picture was successfully created.' if @picture.save
respond_with(@picture)
end
# GET /vehicle/:vehicle_id/pictures/:id
def show
@picture = @vehicle.vehicle_pictures.find(params[:id])
respond_with(@picture)
end
# DELETE /vehicle/:vehicle_id/pictures/:id
def destroy
@picture = @vehicle.vehicle_pictures.find(params[:id])
@picture.destroy
respond_with(@picture)
end
end
class车辆图片控制器
当我发布一张新的车辆图片时,create方法按预期调用,但我收到以下错误:
No route matches {:controller=>"vehicle_pictures", :action=>"show", :vehicle_id=>#<VehiclePicture id: 24, created_at: "2011-04-08 15:07:53", updated_at: "2011-04-08 15:07:53", picture: nil, description: "", vehicle_id: 1>}
没有路径匹配{:controller=>“vehicle\u pictures”、:action=>“show”、:vehicle\u id=>#}
如果我将respond\u with
更改为respond\u with(@picture,:location=>vehicle\u picture\u url(@vehicle.id,@picture.id))
一切正常
但据我所知,我不应该这么做。我做错了什么?我也花了很多时间在这件事上。 你应使用:
respond_with(@vehicle, @picture)
在某些情况下,这也是有意义的:
respond_with(:admin,@picture)
记录在这里
respond_with(:admin,@picture)