繁体   English   中英

如何检查数组并与值进行比较?

[英]How can I check in array and compare with a value?

表格:

|perfils|
  |id|  |name|
    1     Administrator
    2     Admin for products

|accesses|
  |id|  |parent_id|  |name|
   1        0       Module Clients  #parent
   2        1         Show Client   ###child from Module Clients
   3        1         New Client    ###child = Module Clients
   4        1         Edit Client   ###child = Module Clients 
   5        0       Module Products #parent
   6        5         Show Product  ###child = Module Products
   7        5         New Product   ###child = Module Products

|perfil_accesses|
  |id|  |access_id| |perfil_id|
   1       1           1
   2       2           1
   3       3           1

控制器 perfil_controller.rb:

def edit
  @perfi = Perfil.find(params[:id])

  @perfil.perfil_accesses.each do |perfil_access|
    @selected_perfil << perfil_access.id
  end 

  @accesses = Grant.where(parent_id: 0).map do |access|
    { parent: access, children: Grant.where(parent_id: access.id) }
  end
end

楷模

#Perfil.rb
  has_many :perfil_accesses

#PerfilAccess.rb
  belongs_to :grant 
  belongs_to :perfil

查看 new.html.erb

<%= form_for(@perfil) do |f| %>
 <%= f.label :name %><br>
 <%= f.text_field :name %>

 <% @grants.each do |access| %>
 <div>
   <input type="checkbox" class="parentCheckBox" /> <%= access[:parent].name %>
   <a onclick="document.getElementById('div_<%= access[:parent].id %>').style.display='';return false;"><img src="https://cdn0.iconfinder.com/data/icons/ie_Bright/512/plus_add_green.png" height="20" width="20"></a>
   <a onclick="document.getElementById('div_<%= access[:parent].id %>').style.display='none';return false;"><img src="https://cdn0.iconfinder.com/data/icons/ie_Bright/512/minus_remove_green.png" height="20" width="20"></a>
   <br/>

   <div id="div_<%= access.id %>" style="display:none;">
    <ul>
     <% access[:children].each do |grant|%>
       <li><input id="access_<%= grant.id %>" name="access_<%= grant.id %>" type="checkbox" class="childCheckBox" /><%= grant.name %></li>
    <% end %>
    </ul>
   </div>
 </div>    
 <% end %>

 <%= f.submit %>
<% end %>

这是问题:我想检查是否已创建。

<input id="access_<%= access[:parent].id %>" name="access_<%= access[:parent].id %>"
  <% if @selected_perfil.include? == access[:parent].id %>
      checked="checked"
  <% end %> 

我试过:

  <input id="access_<%= access[:parent].id %>" name="access_<%= access[:parent].id %>"
  <% if @selected_perfil.id == access[:parent].id %>
      checked="checked"
  <% end %> 

我想检查来自 perfil_accesses 创建的所有访问。

您的代码似乎不完整,因为您从未将@selected_perfil初始化为空数组。

无论如何:您可以使用include?来检查数组中元素的存在include? (正如您可能已经发现的那样)

a = [1,2,3,4]
a.include?(1) # => true
a.include?(7) # => false

现在,在您的代码中,您尝试将不存在的数组的id (除非您运行旧版本的 ruby​​)与一个值进行比较。 你真正想要做的是检查它是否存在于数组中。

@selected_perfil.include?(access[:parent].id)

应该做的伎俩。 只要确保access[:parent].id也是一个整数

一些提示:

如果我理解正确这应该包含所有ids的的perfil_access相关的对象@perfil

您可以将其简化为:

@perfil.perfil_access.map(&:id)

如果您想在纯 SQL 中执行它(当不需要加载 perfil 访问对象时):

@perfil.perfil_access.pluck(:id)

我找到了一种方法来比较刚刚称为视图关系的数组,并将比较与特定值进行比较。

控制器:

def edit
  @perfil = Perfil.find(params[:id])
  @access_selected = PerfilAccess.where('perfil_id= ?',@perfil.id)

  @accesses = Grant.where(parent_id: 0).map do |access|
    { parent: access, children: Grant.where(parent_id: access.id) }
  end
end

查看

<% @perfil_selected.each do |ps| %>
   <% if ps.access_id == access[:parent].id %>
      checked="checked"
   <% end %> 
<% end %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM