辅助列重写在ruby-on-rails上

问题描述 投票:2回答: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很旧,但我必须使用它...请帮忙!

ruby-on-rails ruby activescaffold
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

Dunno为什么StackOverflow选择了这样一个奇怪的代码格式


0
投票

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.