【问题标题】:ActiveAdmin has_many relation record doesnt saveActiveAdmin has_many 关系记录不保存
【发布时间】:2014-06-25 21:34:58
【问题描述】:

我正在尝试保存具有类别关系的帖子。 这些是我的模型

class Post < ActiveRecord::Base
  has_many :categorizes
  has_many :post_categories, :through=>:categorizes
  accepts_nested_attributes_for :post_categories
end

class PostCategory < ActiveRecord::Base
  has_many :categorizes
  has_many :posts, :through=>:categorizes
end

class Categorize < ActiveRecord::Base
    belongs_to :post
    belongs_to :post_category
end

在 ActiveAdmin post.rb 中。

ActiveAdmin.register Post do
  permit_params :title, :content, post_category_ids:[:id]


  index do
    selectable_column
    id_column
    column :title
    column :post_category_id
    column :created_at

    actions
  end

  filter :created_at

  form do |f|
    f.inputs "Post Details" do
      f.input :title
      f.input :content,:input_html => { :class => "tinymce_editor" }
      #f.input :post_categories, :as=> :check_boxes#, :collection => PostCategory.all

    end

    f.has_many :post_categories ,new_record: false do |c|
        c.inputs do
          c.input :title
        end
      end

    f.actions
  end

  controller do
      defaults :finder => :find_by_slug_url
  end

end

我需要从 post_categories 中查看我的所有类别,并且我应该选择多个。 我检查了 Rails 控制台,但帖子没有任何类别。

Post.First.post_categories equal to []

【问题讨论】:

    标签: ruby-on-rails ruby activeadmin


    【解决方案1】:

    尝试post_category_attributes 而不是post_category_ids

    查看更多here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多