簡體   English   中英

ActiveAdmin:不保存我的兩個模型之間的關聯

[英]ActiveAdmin: Not saving association between my two models

如何將兩個模型之間的關聯保存在ActiveAdmin中?

我有兩個模型,一個房間和一個照片模型。 在ActiveAdmin中,我想更新照片與房間之間的關聯。

room.rb(應用程式/模型)

class Room < ApplicationRecord
    has_many :photos
    accepts_nested_attributes_for :photos
end

photo.rb(應用程式/模型)

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(應用程序/管理員)

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:無? 我已經嘗試了所有似乎沒有任何效果的東西。 請幫忙。

更新

我在photo.rb(admin)的參數中添加了“ rooms_attributes:[:id,:listing_name,:room_id,:photo_id,:images]”。 我也已經將:photo_id添加到room.rb(admin)文件中。

但這仍然行不通! 任何提示歡迎! 如果有人需要進一步的信息,請告訴我。

嘗試

permit_params :image, room_id

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM