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.