Rails:无法找到带ID的嵌套资源

时间:2017-09-17 20:24:20

标签: ruby-on-rails nested-forms

我有一个3层嵌套模型: 画廊 - >有很多专辑 - >有很多照片

当我尝试从这里添加新专辑时 http://localhost:3000/galleries/1/albums/new 我正在

  AlbingsController中的

" ActiveRecord :: RecordNotFound #create Couldn' t   找到带有' id' ="

的图库

专辑模型:

class Album < ApplicationRecord
 belongs_to :gallery
 has_many :photos
  accepts_nested_attributes_for :photos

图库模型

   class Gallery < ApplicationRecord
   has_many :albums
  accepts_nested_attributes_for :albums
 end

路线

....
 resources :galleries do
  resources :albums 
 end
resources :photos, only: [:index, :new, :create, :destroy]
 resources :photos, only: [:index]
 resources :albums  do
   resources :photos, :controller => "albums"
 end

为了进入创建相册的页面,我在图库的“显示”页面中调用它       &lt;%= link_to&#39; New Album&#39;,new_gallery_album_path(@ galleries.id)%&gt; 这会导致正确的页面在链接中具有库的ID 1

但是在控制器中,我真的不明白为什么gallery_id被称为nil

  def new
   @gallery = Gallery.find(params[:gallery_id])
   @album = Album.new
   @photos = @album.photos.build
  end
 def create
  @gallery = Gallery.find(params[:gallery_id])
  @album = Album.new(album_params)

  respond_to do |format|

...

我可以粘贴控制器的其余部分,但现在错误指向create函数中的行 @gallery = Gallery.find(params [:gallery_id]) 为什么这在新的内容中可以正常工作但不适用于创作?

1 个答案:

答案 0 :(得分:2)

在我问之后,实际上想通了。在表格中,我只是打电话

  %= form_for @album], :html => { :mutiplart...

但是在将其改为

之后
  <%= form_for [@gallery,@album], :html => { :mu

有效

相关问题