ActiveAdmin:不保存我的两个模型之间的关联

时间:2016-10-04 11:53:46

标签: ruby-on-rails activeadmin

如何将我的两个模型之间的关联保存在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控制台中的记录(最后一个房间)时它不会将其保存到数据库中它总是返回我room_id:nil?我尝试过似乎没什么用。请帮忙。

更新

我在photo.rb(管理员)的参数中添加了“rooms_attributes:[:id,:listing_name,:room_id,:photo_id,:images]”。我还添加了:photo_id到room.rb(admin)文件。

但它仍然无效!欢迎任何提示!如果有人需要进一步的信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

尝试

permit_params :image, room_id

f.collection_select :room_id, Room.all, :id, :listing_name