2012-09-04 176 views
2

我有基於數據庫結構構建GUI的帶有activescaffold的ruby-on-rails應用程序。 用戶有角色,每個角色是一組權限。每個權限都是控制器和用戶被允許在該控制器中執行的操作的組合。ruby​​-on-rails中的幫助列覆蓋

# 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是老了,但我不得不使用它...請幫助!

回答

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上選擇了這樣一個奇怪的代碼格式