多层复杂形式与formtastic?

时间:2010-12-09 22:50:19

标签: ruby-on-rails-3 formtastic

我的场景需要一个非常复杂的形式,我需要帮助。

我有三张桌子

create_table "permissions", :force => true do |t|
    t.boolean  "can_read"
    t.boolean  "can_create"
    t.boolean  "can_edit"
    t.boolean  "can_delete"
    t.integer  "role_id"
    t.integer  "resource_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

create_table "resources", :force => true do |t|
    t.string   "class_name"
    t.string   "class_action"
    t.text     "description"
    t.integer  "parent_resource"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

create_table "roles", :force => true do |t|
    t.string   "name"
    t.text     "description"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

使用模型和关联

class Role < ActiveRecord::Base
  has_many :user_roles
  has_many :users, :through => :user_roles
  has_many :permissions

  def to_s
    self.name
  end
end

class Resource < ActiveRecord::Base
  has_many :permissions
  has_many :children, :class_name => "Resource", :foreign_key => "parent_resource"

  scope :root, lambda {
    {
      :conditions => "parent_resource IS NULL"
    }
  }
end

class Permission < ActiveRecord::Base
  belongs_to :role
  belongs_to :resource
end

假设我们有2个角色,admin,user,这次,我需要一个表单结构,如this link

中的图像

我如何制作此表格?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我创建了一个gem,可以更轻松地处理formtastic中的嵌套表单:formtastic_cocoon

这应该让你开始。