Ruby on rails 3 了解Rails 3中的路由

Ruby on rails 3 了解Rails 3中的路由,ruby-on-rails-3,nested-routes,Ruby On Rails 3,Nested Routes,我想我考虑的路线都错了。我有一个非常简单的两个模型设置:产品和照片。产品有很多:照片,照片属于:产品 产品有一个完整的脚手架,而Photo有一个我正在处理的photos\u控制器 在routes.rb中,我们有: 资源:产品(由脚手架生成) 由于照片是产品的嵌套资源,我将其更改为: resources :products do resources :photos end 最后: root :to => "products#index" 高兴地说: products GE

我想我考虑的路线都错了。我有一个非常简单的两个模型设置:产品和照片。产品有很多:照片,照片属于:产品

产品有一个完整的脚手架,而Photo有一个我正在处理的photos\u控制器

在routes.rb中,我们有:
资源:产品
(由脚手架生成)

由于照片是产品的嵌套资源,我将其更改为:

resources :products do
    resources :photos
  end
最后:

root :to => "products#index"
高兴地说:

  products GET             {:controller=>"products", :action=>"index"}
  products POST            {:controller=>"products", :action=>"create"}
  new_product GET          {:controller=>"products", :action=>"new"}
  edit_product GET         {:controller=>"products", :action=>"edit"}
  product GET              {:controller=>"products", :action=>"show"}
  product PUT              {:controller=>"products", :action=>"update"}
  product DELETE           {:controller=>"products", :action=>"destroy"}
  product_photos GET       {:controller=>"photos", :action=>"index"}
  product_photos POST      {:controller=>"photos", :action=>"create"}
  new_product_photo GET    {:controller=>"photos", :action=>"new"}
  edit_product_photo GET   {:controller=>"photos", :action=>"edit"}
  product_photo GET        {:controller=>"photos", :action=>"show"}
  product_photo PUT        {:controller=>"photos", :action=>"update"}
  product_photo DELETE     {:controller=>"photos", :action=>"destroy"}
  products GET             {:controller=>"products", :action=>"index"}
  products POST            {:controller=>"products", :action=>"create"}
  new_product GET          {:controller=>"products", :action=>"new"}
  edit_product GET         {:controller=>"products", :action=>"edit"}
  product GET              {:controller=>"products", :action=>"show"}
  product PUT              {:controller=>"products", :action=>"update"}
  product DELETE           {:controller=>"products", :action=>"destroy"}
  root                     {:controller=>"products", :action=>"index"}
这意味着products/new中的表单将发布到products#create,然后重定向到photos#new,并有一个表单用于上传由相应photos/new.html.erb生成的产品照片,该表单将发布到photos#create,对吗

在product_controller.rb中:

def create
    @product = Product.new(params[:product])

    respond_to do |format|
      if @product.save
        redirect_to new_product_photo_path, :notice => 'Product was successfully created.'
      else
        render :action => "new"
      end
    end
  end
在照片_controller.rb中(目前):

为什么我会得到:

Routing Error

No route matches {:controller=>"photos", :action=>"new"}
当rake routes明确表示我愿意时,我有一个photos\u控制器,photos\u控制器中有一个新动作,新产品\u photo\u path显然要求走正确的道路?(我还有一个photos/new.html.erb,它有一个简单的
photos
,可以用来渲染一些东西)

我只能得出这样的结论:我对这个问题的想法是错误的,或者我在常规配置中犯了一个我看不到的错误

有人吗

谢谢和亲切的问候, Adam

更新了答案: 使用嵌套资源意味着(在本例中)只能在产品上下文中创建新照片。这意味着应用程序必须知道要创建的照片属于哪个产品

在重定向的情况下,这意味着您必须将产品对象作为参数添加到
新的\u产品\u照片\u路径中

redirect_to new_product_photo_path(@product)

原始答复:
这是因为您将其设置为嵌套资源<代码>/products/1/photos/new可能确实有效。如果你想通过
/photos/new
创建新照片,你也必须添加一个“未列出的”资源。

谢谢你的回答,Cap'n T。我最初有:照片作为未列出的资源,效果很好。我的问题实际上是更好地理解rails中的路由(可能还有URL和RESTful设计),而不是解决这样的问题。从概念上讲,照片是产品的嵌套资源,rake routes说,新的产品照片应该引导我在照片控制器内执行新操作。我没有指定任何地方是photos/new或/products/1/photos/new,我正在使用控制器重定向到new_product_photo,看不出它为什么不起作用。啊,对不起,我没有仔细阅读你的问题。我会更新我的答案。这在概念上更有意义。非常感谢您抽出时间回答。尝试它会给出预期的重定向(重定向到),但失败了,出现HTTP(我认为)错误(在85毫秒内完成406不可接受)。有什么想法吗?但是,在地址栏中剪切“n”粘贴/products/27/photos/new可以让我找到正确的位置,所以不同的问题?不太担心-在我屠杀脚手架生成的控制器时,有了额外的“结束”。再次感谢东京队长。
redirect_to new_product_photo_path(@product)