ActiveAdmin不会保存很多并且属于很多字段 [英] ActiveAdmin won't save has many and belongs to many field

查看:68
本文介绍了ActiveAdmin不会保存很多并且属于很多字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个模型。类别和职位。它们使用has_many_and_belongs_to_many关系进行连接。我在Rails控制台中签入,关系正常。

I have 2 models. Category and Post. They are connected using a has_many_and_belongs_to_many relationship. I checked in the rails console and the relationship works.

我在activeadmin中创建了复选框,以使用以下表单字段设置帖子类别:

I created checkboxes in activeadmin to set the post categories using this form field:

f.input :categories, as: :check_boxes, collection: Category.all

问题是当我尝试保存它时是因为保存了所有其他字段数据(标题,正文,元信息等),但是即使我未选中它,或者也选中了另一个,该类别也保持不变。

The problem is when I try to save it because every other field data (title, body, meta infos etc.) is saved, but the category stays the same even if I unchecked it, or checked another too.

我正在使用像这样的强参数:

I am using strong parameters like this:

post_params = params.require(:post).permit(:title,:body,:meta_keywords,:meta_description,:excerpt,:image,:categories)

请给我一些建议,使活跃的管理员也可以保存类别!

Please give me some suggestions to make active admin save the categories too!

最好的祝愿,
马特

Best Wishes, Matt

推荐答案

在AA中尝试一下:

    controller do
      def permitted_params
        params.permit post: [:title, :body, :meta_keywords, :meta_description, :excerpt, :image, category_ids: []]
      end
    end

这篇关于ActiveAdmin不会保存很多并且属于很多字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆