创建嵌套资源时,Rails 路由错误与 respond_with

发布于 2024-10-31 18:22:51 字数 3074 浏览 1 评论 0原文

我正在创建的简单 Rails 3 应用程序遇到以下问题。这让我抓狂。

我的模型:

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 paths输出:

    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

当我发布新的车辆图片时,创建方法会按预期调用,但我收到此错误:

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

如果我更改 < code>respond_with 到 respond_with(@picture, :location =>vehicle_picture_url(@vehicle.id, @picture.id)) 事情有效。

但据我了解,我不应该这样做。我做错了什么?

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

  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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉目亦如画i 2024-11-07 18:22:51

还花很多时间在那个东西上。
你应该使用:

  respond_with(@vehicle, @picture)

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

  respond_with(@vehicle, @picture)
相思故 2024-11-07 18:22:51

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

respond_with(:admin,@picture)

In some cases, this also make sense:

respond_with(:admin,@picture)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文