ActiveAdmin / Devise:为User创建has_many关系和表单

时间:2014-05-14 16:41:55

标签: ruby-on-rails devise activeadmin

我在this tutorial之后设置了ActiveAdmin和Devise。

然后我创建了另一个类和我的用户类的has_many关系:

class Household < ActiveRecord::Base
    has_many :users
    accepts_nested_attributes_for :users
end

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  belongs_to :household
  accepts_nested_attributes_for :household
end

然后我创建了一个新的迁移:

class AddHouseholdIdToUsers < ActiveRecord::Migration
  def change
    add_column :users, :household_id, :integer
  end
end

这种关系很好。我能做到:

@user = User.find(1)
@user.household_id = 1
@user.save

它有效。我的app / admin / household.rb文件似乎没问题(看起来像这样:)

ActiveAdmin.register Household do

permit_params :name, :users

index do
  column :name
  column :users do |h|
    table_for h.users.order('email ASC') do
      column do |u|
        u.email
      end
    end
  end
  default_actions
end

show do
  attributes_table do
    row :name
    table_for household.users.order('email ASC') do |t|
      t.column("Email") { |user| user.email }
    end
  end
end

form do |f|
  f.inputs "Add/Edit Household" do
    f.input :name
    f.input :users, :as => :check_boxes, :label => "Email"
  end
  f.actions
end

end

我可以查看,编辑等。

我的问题是,这同样适用于我的app / admin / user.rb文件。它目前看起来像这样:

ActiveAdmin.register User do

    permit_params :email, :household

  index do
    column :email
    column :household

    default_actions
  end

  form do |f|
    f.inputs "User Details" do
      f.input :email     
      f.input :household
    end
    f.actions
  end

  show do
    attributes_table :email, :household
  end
end

索引和显示工作正常,但当我尝试通过ActiveAdmin界面编辑或创建新用户时,出现以下错误:

  Rendered ./.bundler/ruby/1.9.1/active_admin-fdc6a3d9faee/app/views/active_admin/resource/edit.html.arb (70.1ms)
Completed 500 Internal Server Error in 78ms

ActionView::Template::Error (Unable to find input class for ):
    1: insert_tag renderer_for(:edit)
  app/admin/user.rb:21:in `block (3 levels) in <top (required)>'
  app/admin/user.rb:18:in `block (2 levels) in <top (required)>'

如果我离开app / admin / user.rb empy,我会得到:

Completed 500 Internal Server Error in 135ms

ActionView::Template::Error (Unable to find input class for ):
    1: insert_tag renderer_for(:edit)
  ./.bundler/ruby/1.9.1/active_admin-fdc6a3d9faee/lib/active_admin/form_builder.rb:132:in `rescue in input_class'
  ./.bundler/ruby/1.9.1/active_admin-fdc6a3d9faee/lib/active_admin/form_builder.rb:121:in `input_class'
[...]

我在网上搜索了一个解决方案,但找不到任何东西。我完全卡住了。有没有人知道问题可能是什么?

//编辑:如果我做同样的事情,但使用不同于用户的类,我不会得到错误。我可以编辑和创建该类,我可以在表单中看到关系。但保存表单时不会保存关系。

// Edit2:在非用户类的情况下:如果我不仅允许类作为参数,而且还允许class_id,它可以工作。所以在我的例子中,它将是:

permit_params :email, :household, :household_id

但这只适用于非用户类,所以我的问题仍然存在:(

0 个答案:

没有答案