ruby-on-rails-3 - 创建嵌套资源时,Rails 路由错误与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

我的路线.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

当我发布新的车辆图片时,会按预期调用 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>}

如果我更改 respond_withrespond_with(@picture, :location => vehicle_picture_url(@vehicle.id, @picture.id))事情工作。

但据我所知,我不应该这样做。我究竟做错了什么?

最佳答案

也花很多时间在这件事上。
你应该使用:

  respond_with(@vehicle, @picture)

关于ruby-on-rails-3 - 创建嵌套资源时,Rails 路由错误与respond_with,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5597218/

相关文章:

ruby-on-rails - Rails 5 - 带路由的嵌套 Controller

javascript - React Router v4 全局不匹配嵌套路由子项

xml - Rails Controller 处理为 HTML 而不是 XML

ruby-on-rails - 即使查询正确,Activerecord也无法按属性查找模型

ruby-on-rails-3 - Rails 3.1 asset pipeline CSS 缓存正在开发中

ruby-on-rails - Rails 3 + PDFKit 问题:权限被拒绝 (Errno::EACCES)

ruby-on-rails - Heroku值得吗?

ruby-on-rails - Rails 4嵌套了资源,但没有公开父级的RESTful路由?

ruby-on-rails - 阻止 Rails 尝试提供模板/ActionView::MissingTemplate