i have three model project_manager, project_director, and human_resource each has a status Boolean field how can i print some thing in rails if Boolean value of these three model is true. currently i am accessing data from model by doing this-
<% if project_site.project_managers.empty? %>
<td class="pending fi-eye"><%= " Pending" %></td>
<% else %>
<% project_site.project_managers.each do |project_manager| %>
<% if project_manager.status == false %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% elsif project_manager.status == true %>
<td class="approved fi-check"><%= " Approved" %></td>
<% end %>
<% end %>
<% end %>
<% if project_site.project_directors.empty? %>
<td class="pending fi-eye"><%= " Pending" %></td>
<% else %>
<% project_site.project_directors.each do |project_director| %>
<% if project_director.status == false %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% elsif project_director.status == true %>
<td class="approved fi-check"><%= " Approved" %></td>
<% end %>
<% end %>
<% end %>
<% if project_site.human_resources.empty? %>
<td class="pending fi-eye"><%= " Pending" %></td>
<% else %>
<% project_site.human_resources.each do |human_resource| %>
<% if human_resource.status == false %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% elsif human_resource.status == true %>
<td class="approved fi-check"><%= " Approved" %></td>
<% end %>
<% end %>
<% end %>
i want to print approved if all these three model status value is true how can i do that in rails?
Create a helper method and put the following code :
def check_resource_status(project_site, resources)
statuses = project_site.send(resources.to_sym).pluck(:status)
statuses.all? ? true : false
end
def status_container(status)
content_tag :div, class: ['sample'] do
status_label = status ? 'Approved' : 'Rejected'
default_class = status ? 'fi-check' : 'fi-x'
status_class = [default_class, status_label.downcase]
concat content_tag(:label, status_label, class: status_class)
end
end
From your view file :
status_container(check_resource_status(project_site, 'human_resources'))
status_container(check_resource_status(project_site, 'project_directors'))
status_container(check_resource_status(project_site, 'project_managers'))
If I understood your question correctly, all you need is something like this:
<% if human_resource.status? %>
<td class="approved fi-check"><%= ' Approved' %></td>
<% else %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% 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.