在视图页面栏中的父复选框下显示子复选框

时间:2012-10-26 12:50:45

标签: ruby-on-rails

  

我在视图页面显示复选框,数据将来自数据库。这是我的代码,问题是,如果同一个父名称出现循环,它会两次显示相同的父复选框。

<% @permission.each do |f| %>
        <% if @controller_code != f.controller_code %>
            <% @controller_code = f.controller_code %>
            <%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false,  :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
            <br/>
            <% @permission.each do |f| %>
                <% if @controller_code == f.controller_code %>
                    &nbsp;  &nbsp;<%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false,  :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %> <%= f.action_name %>
                    <br/>
                <% end %>
            <% end %>   
        <% end %>
  

我也尝试过这种方式,但它无法正常工作..

<% @permission.each do |f| %>   
            <% if @controller_code != f.controller_code %>
            <% @controller_code = f.controller_code %>
                <%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false,  :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
             <br/>
            <% end %>
            &nbsp;  &nbsp;<%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false,  :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %> <%= f.action_name %>
            <br/>        
<% end %>

2 个答案:

答案 0 :(得分:0)

我不知道我为你的问题给出了确切的答案,我认为这可以帮到你,

<% @permission.each do |f| %>
    <% if @controller_code != f.controller_code %>
        <% @controller_code = f.controller_code %>
        <%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false,  :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
    <% else %>
      <%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false,  :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %>        
      <%= f.action_name %>
    <% end %>
<% end %>

答案 1 :(得分:0)

  

我在这里找到了我的问题的解决方案,它在控制器中执行以下操作

 @permission=Permission.all.where(active: 1)
if @permission.blank? == false
  @permissions =  Permission.all.where(active: 1).order_by(:controller_code => "asc")
else
  @permissions = ""
end
  

在视图页面中执行以下操作

<% @permissions.each do |f| %>
        <% if @controller_code != f.controller_code %>
            <% @controller_code = f.controller_code %>
            <%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false,  :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
            <br/>
            <% @permissions.each do |f| %>
                <% if @controller_code == f.controller_code %>
                    &nbsp;  &nbsp;<%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false,  :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %> <%= f.action_name %>
                    <br/>
                <% end %>
            <% end %>   
        <% end %>
    <% end %>