如何在 Rails 5.0.1 中使用 Active Admin、Paperclip 创建带有表单选择字段的两个模型之间的关联
How to create an association between two models with form choice field using Active Admin, Paperclip in Rails 5.0.1
场景
我正在构建类似 AirBnB 的应用程序,但不同之处在于只有管理员可以添加新的公寓、房间、房屋等。因此我创建了一个管理面板,其中包含 activeadmin gem。我现在正在将图像上传系统添加到 activeadmin 中,所以我正在使用回形针 gem。我有两个模型 "room" 和 "photo"。照片模型有一个 "image" 列。房间模型有 "listing_name" 列。
room.rb (app/models)
class Room < ApplicationRecord
has_many :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)" 我可以默认将图像关联到房间。但是我不能上传图片!这看起来像这样:
所以我将 "photo.rb (app/admin)" 文件更改为:
photo.rb (app/admin)
ActiveAdmin.register Photo do
permit_params :image
form :html => {:multipart => true} do |f|
f.inputs "Project Details" do
f.input :image, :required => false, :as => :file
end
f.actions
end
show do |ad|
attributes_table do
row :image do
image_tag(ad.image.url(:thumb))
end
end
end
end
使用此表单,我可以将图片上传到图片列,但无法将其关联到房间,而且编辑视图无法像显示视图那样提供图片预览:
问题:
如何在管理编辑视图中预览图像?
如何通过选择房间的listing_name来构建一个将图像关联到房间的表格?
像这样:
预先感谢您的帮助。
尝试 https://github.com/gregbell/active_admin/issues/599 然后 Macfanatics 回答。它描述了设置多态关联以及如何在 activeadmin 中使用它。它对我帮助很大。
解决方案:
photo.rb
...
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
row :room_id do
photo.room ? photo.room.listing_name : ""
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_id, Room.all,:id,:listing_name
end
f.actions
end
场景
我正在构建类似 AirBnB 的应用程序,但不同之处在于只有管理员可以添加新的公寓、房间、房屋等。因此我创建了一个管理面板,其中包含 activeadmin gem。我现在正在将图像上传系统添加到 activeadmin 中,所以我正在使用回形针 gem。我有两个模型 "room" 和 "photo"。照片模型有一个 "image" 列。房间模型有 "listing_name" 列。
room.rb (app/models)
class Room < ApplicationRecord
has_many :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)" 我可以默认将图像关联到房间。但是我不能上传图片!这看起来像这样:
所以我将 "photo.rb (app/admin)" 文件更改为:
photo.rb (app/admin)
ActiveAdmin.register Photo do
permit_params :image
form :html => {:multipart => true} do |f|
f.inputs "Project Details" do
f.input :image, :required => false, :as => :file
end
f.actions
end
show do |ad|
attributes_table do
row :image do
image_tag(ad.image.url(:thumb))
end
end
end
end
使用此表单,我可以将图片上传到图片列,但无法将其关联到房间,而且编辑视图无法像显示视图那样提供图片预览:
问题:
如何在管理编辑视图中预览图像?
如何通过选择房间的listing_name来构建一个将图像关联到房间的表格?
像这样:
预先感谢您的帮助。
尝试 https://github.com/gregbell/active_admin/issues/599 然后 Macfanatics 回答。它描述了设置多态关联以及如何在 activeadmin 中使用它。它对我帮助很大。
解决方案:
photo.rb
...
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
row :room_id do
photo.room ? photo.room.listing_name : ""
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_id, Room.all,:id,:listing_name
end
f.actions
end