为has_and_belongs_to_many关系自定义活动管理员表单输入 [英] Custom Active Admin form inputs for has_and_belongs_to_many relationship

查看:374
本文介绍了为has_and_belongs_to_many关系自定义活动管理员表单输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的模型

I have a very simple model

class Lifestyle < ActiveRecord::Base
  attr_accessible :name
  has_and_belongs_to_many :profiles
end

这有一个 has_​​and_belongs_to_many 关系简介

class Profile < ActiveRecord::Base
  attr_accessible ...

  belongs_to :occupation

  has_and_belongs_to_many :lifestyles
  accepts_nested_attributes_for :lifestyles
end

我想用ActiveAdmin编辑配置文件对象,但也分配生活方式的配置文件。它应该是类似与处理belongs_to的:职业,因为这是由ActiveAdmin自动排序到保管箱的选项pre-充满了可用的职业。

I want to use ActiveAdmin to edit the Profile object, but also assign Lifestyles to a profile. It should be similar to dealing with belongs_to :occupation, as this is sorted out automatically by ActiveAdmin to a dropbox with the options pre-filled with available occupations.

我试图使用的has_many 表单生成方法,但只让我展现形式的生活方式和提交的名称类型,返回一个错误。

I've tried to use the has_many form builder method, but that only got me to show a form to type in the name of the Lifestyle and on submission, it returned an error.

    f.object.lifestyles.build
    f.has_many :lifestyles do |l|
      l.input :name
    end

错误我得到:

Can't mass-assign protected attributes: lifestyles_attributes

对我来说,完美的办法是多建几个复选框,一个用于在数据库中每一个生活方式。该生活方式被连接到简档而选择的装置,以及未选定的装置删除该关系。

The perfect way for me would be to build several checkboxes, one for each Lifestyle in the DB. Selected means that the lifestyle is connected to the profile, and unselected means to delete the relation.

我有极大的质疑,认为这是可能使用ActiveAdmin而无需创建非常复杂的逻辑来处理这个问题。 我真的AP preciate,如果你想给你的意见和建议我,如果我应该走这条路还是不同的方法处理它。

I'm having great doubts that this is possible using ActiveAdmin and without having to create very complex logic to deal with this. I would really appreciate it if you'd give your opinion and advise me if I should go this way or approach it differently.

推荐答案

经过一番研究,我愿意回答我的问题。

After some research, I am ready to answer my own question.

首先,我要说感谢@Lichtamberg提出了这个补丁。然而,只有复杂的事情(也有关安全,虽然不是在这种情况下一个问题),并没有帮助我达到我的理想解决方案。

First, I have to say thanks to @Lichtamberg for suggesting the fix. However, that only complicates things (also regarding security, though not an issue in this case), and doesn't help me reach my ideal solution.

挖多了,我发现这是Rails的一个非常常见的场景,并且它在 Ryan Ba​​tes的截屏实际上解释没有#17

Digging more, I found out that this is a very common scenario in Rails, and it's actually explained in Ryan Bates' screencast no #17.

因此​​,在Rails的,如果你有一个has_and_belongs_to_many(简式HABTM)的关联,您可以轻松地通过这种方法设置其他关联对象的ID:

Therefore, in Rails, if you have a has_and_belongs_to_many (short form HABTM) association, you can easily set the ids of the other associated object through this method:

profile.lifestyle_ids = [1,2]

这显然适用于表单,如果你已经设置的lifestyle_ids attr_accessible:

And this obviously works for forms if you've set the attr_accessible for lifestyle_ids:

class Profile < ActiveRecord::Base
  attr_accessible :lifestyle_ids
end

在ActiveAdmin,因为它使用 Formtastic ,则可以使用此方法来输出正确的字段(在本例中的复选框):

In ActiveAdmin, because it uses Formtastic, you can use this method to output the correct fields (in this case checkboxes):

f.input :lifestyles, as: :check_boxes, collection: Lifestyle.all

另外,我还简化了我的表单视图所以它现在仅仅是这样的:

Also, I have simplified my form view so it's now merely this:

  form do |f|
    f.inputs # Include the default inputs
    f.inputs "Lifestlyes" do # Make a panel that holds inputs for lifestyles
      f.input :lifestyles, as: :check_boxes, collection: Lifestyle.all # Use formtastic to output my collection of checkboxes
    end
    f.actions # Include the default actions
  end

好了,现在这个完美的呈现在视图中,但如果我尝试并提交我的变化,它给了我这个数据库错误:

Ok, now this rendered perfectly in the view, but if I try and submit my changes, it gives me this database error:

PG::Error: ERROR:  null value in column "created_at" violates not-null constraint
: INSERT INTO "lifestyles_profiles" ("profile_id", "lifestyle_id") VALUES (2, 1) RETURNING "id"

我发现,这是由于Rails的3.2不会自动更新为HABTM关联表的时​​间戳(因为它们是额外的属性,和Rails只处理 _id 属性。

有两个解决方法来解决这个问题:

There are 2 solutions to fix this:


  1. 要么转换协成一个HM:T(的has_many,:通过=&GT;

  2. 或者从表中删除时间戳

我要去2),因为我永远不会需要时间戳或任何额外的属性。

I'm going to go for 2) because I will never need the timestamps or any extra attributes.

我希望这有助于具有相同问题的其他人。

I hope this helps other people having the same problems.

编辑: @cdesrosiers是最接近的解决方案,但我已经写了这个答案之前,我读他的。无论如何,这是伟大的不过。我学习了很多东西。

@cdesrosiers was closest to the solution but I already wrote this answer before I read his. Anyway, this is great nevertheless. I'm learning a lot.

这篇关于为has_and_belongs_to_many关系自定义活动管理员表单输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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