2

我有带有 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)
    ...

我知道 activescaffold 很旧,但我必须使用它...请帮助!

4

2 回答 2

0

最后我找到了解决方案。这对我有用=)

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 会选择如此奇怪的代码格式

于 2012-09-05T16:21:55.267 回答
0

如果您不需要任何特殊格式,我建议使用

config.columns[:your_column].form_ui = :select

然后将其设为多个(检查文档)。然后使用 :update_config 方法更新配置,并为每个 current_user 应用新选项。

https://github.com/activescaffold/active_scaffold/wiki/Per-Request-Configuration

于 2017-12-19T12:06:17.687 回答