我有一个带有activescaffold的ruby on rails应用程序,可以基于数据库结构构建GUI。用户有角色,每个角色都是一组权限。每项权利都是控制器和允许用户在此控制器中执行或不执行的动作的组合。
# DATABASE!
create_table :rights do |t|
t.column :controller, :string, :limit => 32, :null => false
t.column :action, :string, :limit => 32, :null => false
end
create_table :rights_roles, :id => false do |t|
t.column :right_id, :integer
t.column :role_id, :integer
end
create_table :roles do |t|
t.column :name, :string, :limit => 32, :null => false
end
#MODELS!
class Role < ActiveRecord::Base
has_and_belongs_to_many :rights
class Right < ActiveRecord::Base
has_and_belongs_to_many :roles
# ROLE CONTROLLER!
class RoleController < ApplicationController
active_scaffold :role do |config|
config.columns = Array[:name, :rights]
config.columns[:rights].form_ui = :select
我目前有以下角色的编辑窗口,这是不方便的(选项没有结构。会有更多的操作,所以这将是可怕的):
http://postimage.org/image/4e8ukk2px/
我想创建一个像这样的辅助方法:
module RoleHelper
def rights_form_column (record, input_name)
...
end
end
这是定义表单所必需的,该表单将指定"权限"列的输入方法。但我不知道怎么写。理想的形式如下:
administration
action1(checkbox)
action2(checkbox)
configuration
action1(checkbox)
...
我知道活动支架很旧,但我必须使用它……请帮忙!
终于找到了解决方案。这对我很好=)
require 'set'
module RoleHelper
def rights_form_column (record, input_name)
selected_ids = [].to_set
record.rights.each do |r|
selected_ids.add(r.id)
end
html = '<table style="margin-top:-25; margin-left:112">'
html << '<tr>'
html << '<td>'
html << '<ul style="list-style-type:none">'
Right.find(:all, :group => :controller).each do |right|
unless Right::CONFIGURATION_CONTROLLERS.include?(right.controller) then
html << '<li>'
html << "<label>#{right.controller.titleize}:</label>"
html << '<ul style="list-style-type:none">'
Right.find(:all, :conditions => ["controller = ?", right.controller]).each do |r|
html << '<li>'
this_name = "#{input_name}[#{r.id}][id]"
html << check_box_tag(this_name, r.id, selected_ids.include?(r.id))
html << "<label for='#{this_name}'>"
html << r.action.titleize
html << '</label>'
html << '</li>'
end
html << '</ul>'
html << '</li>'
end
end
html << '<li>' # configuration section
html << "<label>Configuration:</label>"
html << '<ul style="list-style-type:none">'
Right.find(:all, :group => :controller).each do |right|
if Right::CONFIGURATION_CONTROLLERS.include?(right.controller) then
Right.find(:all, :conditions => ["controller = ?", right.controller]).each do |r|
html << '<li>'
this_name = "#{input_name}[#{r.id}][id]"
html << check_box_tag(this_name, r.id, selected_ids.include?(r.id))
html << "<label for='#{this_name}'>"
html << r.controller.titleize + " edit"
html << '</label>'
html << '</li>'
end
end
end
html << '</ul>'
html << '</li>'
html << '</ul>'
html << '</td>'
html << '</tr>'
html << '</table>'
html
end
end
不知道StackOverflow为什么选择这样一个奇怪的代码格式
如果你不需要任何特殊的格式,我建议你使用
config.columns[:your_column].form_ui = :select
然后将其设为多个(检查文档)。然后使用:update_config方法更新config,并为每个current_user应用新选项。
https://github.com/activescaffold/active_scaffold/wiki/Per-Request-Configuration