ActiveAdmin:不保存我的两个模型之间的关联
ActiveAdmin: Not saving association between my two models
我的两个模型之间的关联如何保存在 ActiveAdmin 中?
我有两个模型,一个是房间模型,一个是照片模型。在 ActiveAdmin 中,我想更新照片和房间之间的关联。
room.rb (app/models)
class Room < ApplicationRecord
has_many :photos
accepts_nested_attributes_for :photos
end
photo.rb (app/models)
class Photo < ApplicationRecord
belongs_to :room
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
photo.rb (app/admin)
ActiveAdmin.register Photo do
permit_params :image, , rooms_attributes: [:id, :listing_name, :room_id, :photo_id, :images]
index do
selectable_column
id_column
column :image_file_name
column :listing_name
column :room
column :updated_at
column :created_at
actions
end
show do |image|
attributes_table do
row :image do
photo.image? ? image_tag(photo.image.url, height: '100') : content_tag(:span, "No photo yet")
end
end
active_admin_comments
end
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :image, hint: f.photo.image? ? image_tag(f.photo.image.url, height: '100') : content_tag(:span, "Upload JPG/PNG/GIF image")
end
f.inputs do
f.collection_select :room, Room.all,:id,:listing_name
end
f.actions
end
该表格似乎有效,但当我在 rails 控制台中检查记录(最后一个房间)时,它不会将其保存到数据库中,它总是 returns 我 room_id:零?我已经尝试了一切似乎都没有用。请帮忙。
更新
我已将 "rooms_attributes: [:id, :listing_name, :room_id, :photo_id, :images]" 添加到 photo.rb(admin) 中的参数。我还添加了 :photo_id 到 room.rb(admin) 文件。
但是还是不行!欢迎任何提示!如果有人需要更多信息,请告诉我。
尝试
permit_params :image, room_id
f.collection_select :room_id, Room.all, :id, :listing_name
我的两个模型之间的关联如何保存在 ActiveAdmin 中?
我有两个模型,一个是房间模型,一个是照片模型。在 ActiveAdmin 中,我想更新照片和房间之间的关联。
room.rb (app/models)
class Room < ApplicationRecord
has_many :photos
accepts_nested_attributes_for :photos
end
photo.rb (app/models)
class Photo < ApplicationRecord
belongs_to :room
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
photo.rb (app/admin)
ActiveAdmin.register Photo do
permit_params :image, , rooms_attributes: [:id, :listing_name, :room_id, :photo_id, :images]
index do
selectable_column
id_column
column :image_file_name
column :listing_name
column :room
column :updated_at
column :created_at
actions
end
show do |image|
attributes_table do
row :image do
photo.image? ? image_tag(photo.image.url, height: '100') : content_tag(:span, "No photo yet")
end
end
active_admin_comments
end
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :image, hint: f.photo.image? ? image_tag(f.photo.image.url, height: '100') : content_tag(:span, "Upload JPG/PNG/GIF image")
end
f.inputs do
f.collection_select :room, Room.all,:id,:listing_name
end
f.actions
end
该表格似乎有效,但当我在 rails 控制台中检查记录(最后一个房间)时,它不会将其保存到数据库中,它总是 returns 我 room_id:零?我已经尝试了一切似乎都没有用。请帮忙。
更新
我已将 "rooms_attributes: [:id, :listing_name, :room_id, :photo_id, :images]" 添加到 photo.rb(admin) 中的参数。我还添加了 :photo_id 到 room.rb(admin) 文件。
但是还是不行!欢迎任何提示!如果有人需要更多信息,请告诉我。
尝试
permit_params :image, room_id
f.collection_select :room_id, Room.all, :id, :listing_name