Rails 嵌套属性未显示在 SHOW/not 保存父 ID 中
Rails nested attributes not displayed in SHOW/not saving parent id
当谈到嵌套参数和它们的控制器时,我似乎无法掌握路由的窍门。稍后编辑 - 路线似乎不错,但 parent_id 未保存
在我的路线中我有
resources :galleries do
resources :album
end
然后在控制器中显示
def show
@albums = Album.find(params[:id])
@photos = @albums.photos.all
end
def index
@albums = Album.includes(:photos).all
end
def new
@gallery = Gallery.find(params[:gallery_id])
@album = @gallery.albums.build
@photos = @album.photos.build
end
def create
@gallery = Gallery.find(params[:gallery_id])
@album = @gallery.albums.new(album_params)
respond_to do |format|
if @album.save
params[:photos]['image'].each do |a|
@photo = @album.photos.create!(:image => a, :album_id => @album.id)
end
format.html { redirect_to gallery_path(@gallery.id), notice: 'Post was successfully created.' }
模特是这样的
class Album < ApplicationRecord
belongs_to :gallery
has_many :photos
accepts_nested_attributes_for :photos
end
class Gallery < ApplicationRecord
has_many :albums
accepts_nested_attributes_for :albums
end
在我的画廊表单中,我看到所有画廊列值都很好。
现在我正尝试为初学者显示这样的专辑列表,但没有显示任何内容:
<% @gallery.albums.each do |album| %>
<%= album.name %>
<%= link_to "Destroy", album, method: :delete %>
<% end %>
原来我创建新相册时图库parent_id没有保存
我这样叫专辑创作
<%= link_to 'New Album', new_gallery_album_path(@galleries.id) %>
这导致了这个 url http://localhost:3000/galleries/1/albums
我不确定创建定义中缺少什么以便在我保存相册时保存 gallery_id
稍后编辑
应用@Esther 的建议之前的控制台
Gallery Load (1.0ms) SELECT "galleries".* FROM "galleries" WHERE
"galleries"."id" = LIMIT [["id", 1], ["LIMIT", 1]] (0.0ms)
BEGIN SQL (0.0ms) INSERT INTO "albums" ("band", "gallery_id",
"created_at", "updated_at") VALUES (, , , ) RETURNING "id"
[["ban d", "test"], ["gallery_id", 1], ["created_at", "2017-09-18
21:09:59.805397"], ["updated_at", "2017-09-18 21:09:59.805397"]]
(0.0ms) COMMIT (0.0ms) BEGIN
并且在将控制器修改为使用 @gallery.albums.create(album_params)
而不是 @gallery.albums.create
之后
Gallery Load (0.0ms) SELECT "galleries".* FROM "galleries" WHERE
"galleries"."id" = LIMIT [["id", 1], ["LIMIT", 1]]
(0.0ms) BEGIN
SQL (1.0ms) INSERT INTO "albums" ("band", "gallery_id", "created_at", "updated_at") VALUES (, , , ) RETURNING "id"
[["ban
d", "new"], ["gallery_id", 1], ["created_at", "2017-09-19 18:28:41.093138"], ["updated_at", "2017-09-19 18:28:41.093138"]]
(0.0ms) COMMIT
(0.0ms) BEGIN
(1.0ms) COMMIT
(0.0ms) BEGIN
在我看来,没有任何与此画廊相关联的专辑记录。您可以通过打印计数或调试语句来确认。
# Example:
There are <%= @albums.count %> albums associated with this gallery.
# For debugging only:
<%= debug @albums %>
您也可以在控制台中看到这一点。
@gallery.albums.count
@gallery.albums.any?
如果您认为有问题,您可以直接检查数据库中的记录(运行 rails db
使用相同的凭据连接到您的数据库)然后您可以执行 SQL:
SELECT COUNT(*) FROM albums WHERE gallery_id = 1;
"parent_id is not saved" 是指 gallery_id
没有保存到 album
吗?
如果是,我的第一个想法是我认为您应该将相册创建方法中的这行代码从 @album = @gallery.albums.new(album_params)
更改为 @album = @gallery.albums.create(album_params)
。这会立即使用 album_params
中的现有参数创建相册,我希望在您继续将照片添加到相册之前,其中包含 gallery_id
。
其次,能否请您使用控制台的日志更新您的问题,以便我们确认 album_params
.
中的内容
当谈到嵌套参数和它们的控制器时,我似乎无法掌握路由的窍门。稍后编辑 - 路线似乎不错,但 parent_id 未保存
在我的路线中我有
resources :galleries do
resources :album
end
然后在控制器中显示
def show
@albums = Album.find(params[:id])
@photos = @albums.photos.all
end
def index
@albums = Album.includes(:photos).all
end
def new
@gallery = Gallery.find(params[:gallery_id])
@album = @gallery.albums.build
@photos = @album.photos.build
end
def create
@gallery = Gallery.find(params[:gallery_id])
@album = @gallery.albums.new(album_params)
respond_to do |format|
if @album.save
params[:photos]['image'].each do |a|
@photo = @album.photos.create!(:image => a, :album_id => @album.id)
end
format.html { redirect_to gallery_path(@gallery.id), notice: 'Post was successfully created.' }
模特是这样的
class Album < ApplicationRecord
belongs_to :gallery
has_many :photos
accepts_nested_attributes_for :photos
end
class Gallery < ApplicationRecord
has_many :albums
accepts_nested_attributes_for :albums
end
在我的画廊表单中,我看到所有画廊列值都很好。 现在我正尝试为初学者显示这样的专辑列表,但没有显示任何内容:
<% @gallery.albums.each do |album| %>
<%= album.name %>
<%= link_to "Destroy", album, method: :delete %>
<% end %>
原来我创建新相册时图库parent_id没有保存
我这样叫专辑创作
<%= link_to 'New Album', new_gallery_album_path(@galleries.id) %>
这导致了这个 url http://localhost:3000/galleries/1/albums
我不确定创建定义中缺少什么以便在我保存相册时保存 gallery_id
稍后编辑
应用@Esther 的建议之前的控制台
Gallery Load (1.0ms) SELECT "galleries".* FROM "galleries" WHERE "galleries"."id" = LIMIT [["id", 1], ["LIMIT", 1]] (0.0ms) BEGIN SQL (0.0ms) INSERT INTO "albums" ("band", "gallery_id", "created_at", "updated_at") VALUES (, , , ) RETURNING "id" [["ban d", "test"], ["gallery_id", 1], ["created_at", "2017-09-18 21:09:59.805397"], ["updated_at", "2017-09-18 21:09:59.805397"]]
(0.0ms) COMMIT (0.0ms) BEGIN
并且在将控制器修改为使用 @gallery.albums.create(album_params)
而不是 @gallery.albums.create
Gallery Load (0.0ms) SELECT "galleries".* FROM "galleries" WHERE "galleries"."id" = LIMIT [["id", 1], ["LIMIT", 1]] (0.0ms) BEGIN SQL (1.0ms) INSERT INTO "albums" ("band", "gallery_id", "created_at", "updated_at") VALUES (, , , ) RETURNING "id" [["ban d", "new"], ["gallery_id", 1], ["created_at", "2017-09-19 18:28:41.093138"], ["updated_at", "2017-09-19 18:28:41.093138"]] (0.0ms) COMMIT (0.0ms) BEGIN (1.0ms) COMMIT (0.0ms) BEGIN
在我看来,没有任何与此画廊相关联的专辑记录。您可以通过打印计数或调试语句来确认。
# Example:
There are <%= @albums.count %> albums associated with this gallery.
# For debugging only:
<%= debug @albums %>
您也可以在控制台中看到这一点。
@gallery.albums.count
@gallery.albums.any?
如果您认为有问题,您可以直接检查数据库中的记录(运行 rails db
使用相同的凭据连接到您的数据库)然后您可以执行 SQL:
SELECT COUNT(*) FROM albums WHERE gallery_id = 1;
"parent_id is not saved" 是指 gallery_id
没有保存到 album
吗?
如果是,我的第一个想法是我认为您应该将相册创建方法中的这行代码从 @album = @gallery.albums.new(album_params)
更改为 @album = @gallery.albums.create(album_params)
。这会立即使用 album_params
中的现有参数创建相册,我希望在您继续将照片添加到相册之前,其中包含 gallery_id
。
其次,能否请您使用控制台的日志更新您的问题,以便我们确认 album_params
.