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 %>
            <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 %>
4

2 回答 2

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 %>
于 2012-10-30T04:54:38.757 回答
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 %>
于 2012-10-26T13:36:30.963 回答