2012-09-04 6 views
2

데이터베이스 구조를 기반으로 GUI를 빌드하기 위해 activescaffold가있는 루비 온 레일 앱이 있습니다. 사용자에게는 역할이 있으며 각 역할은 권한 집합입니다. 각 권한은 사용자가이 컨트롤러에서 수행 할 수 있는지 여부와 컨트롤러의 조합입니다.루비 온 레일 (well-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 당신이 특별한 형식을 필요가없는 경우에, 나는 그것을 만들 다음

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

를 사용하는 것이 좋습니다 것입니다

관련 문제