1

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.

2
  • can you give a little more detail about what's not working with your code? Commented Apr 8, 2016 at 2:14
  • The selected check is not checking all that were created Commented Apr 8, 2016 at 3:02

2 Answers 2

1

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) 
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.