开发者

Rails routing error with respond_with when creating nested resource

开发者 https://www.devze.com 2023-02-22 21:06 出处:网络
I\'m having the following problem with a simple Rails 3 app I\'m creating.It is driving me batty. My model:

I'm having the following problem with a simple Rails 3 app I'm creating. It is driving me batty.

My model:

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

class VehiclePicture < ActiveRecord::Base
   belongs_to :vehicle
end

My routes.rb:

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

My rake routes output:

    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

  priva开发者_开发技巧te
  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

When I post a new vehicle picture the create method is invoked as expected but I get this error:

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

If I change the respond_with to respond_with(@picture, :location => vehicle_picture_url(@vehicle.id, @picture.id)) things work.

But from what I understand I shouldn't have to do this. What am I doing wrong?


Also spend a lot of time with that thing. You should use:

  respond_with(@vehicle, @picture)


In some cases, this also make sense:

respond_with(:admin,@picture)
0

精彩评论

暂无评论...
验证码 换一张
取 消