2011-04-08 110 views
15

我正在创建一个简单的Rails 3应用程序时遇到以下问题。它在驾驶着我。在创建嵌套资源时Rails路由与respond_with错误

我的模型:

class Vehicle < ActiveRecord::Base 
    has_many :vehicle_pictures, :dependent => :destroy 
def 

class VehiclePicture < ActiveRecord::Base 
    belongs_to :vehicle 
end 

我的routes.rb:

MyApp::Application.routes.draw do 
    resources :vehicles do 
     resources :pictures, :controller => :vehicle_pictures 
    end 
end 

rake routes输出:

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"} 

我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 

当我发布一个新的车辆图片为预期的创建方法被调用,但我得到这个错误:如果我改变respond_withrespond_with(@picture, :location => vehicle_picture_url(@vehicle.id, @picture.id))工作的事情

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>} 

但从我的理解我不应该这样做。我究竟做错了什么?

回答

38

也花了很多时间与那件事。 你应该使用:

respond_with(@vehicle, @picture) 
+0

谢谢你很多!需要接受! – 2012-08-20 13:44:48

+0

我爱你,但这是记录在哪里!?太令人沮丧了...... – 2012-10-18 01:53:29

+1

这里记录了http://api.rubyonrails.org/classes/ActionController/Responder.html – malclocke 2012-11-13 01:11:24

2

在某些情况下,这也是有道理的:

respond_with(:admin,@picture)