简体   繁体   中英

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

Tables:

|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

Controller 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

Models

#Perfil.rb
  has_many :perfil_accesses

#PerfilAccess.rb
  belongs_to :grant 
  belongs_to :perfil

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

Here is the problem: I want to check if was created.

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

I tried:

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

I want to check all accesses from perfil_accesses created.

Your code does not seem to be complete since you never initialize @selected_perfil to be an empty array.

Anyways: you can check the existence of an element in an array with include? (as you perhaps already discovered)

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

Now in your code you try to compare the id of the array which does not exist (unless you run older versions of ruby) with a value. What you actually want to do is to check if it exists inside the array.

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

should do the trick. Just make sure that access[:parent].id is also an integer

Some hints:

If I understood correct this should contain all ids of the perfil_access objects associated to the @perfil ?

You can simplify this to:

@perfil.perfil_access.map(&:id)

And in cases where you want to execute it in pure SQL (when the perfil access objects not need to be loaded):

@perfil.perfil_access.pluck(:id)

I found a way to compare an array just called the view relationship and the compare with a specific value.

Controller:

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

View :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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