Rails - Inserting select elements from controller in partial - ruby-on-rails

(Very new to rails and bootstrap) I'm working on a project in which I want to insert elements into an html accordion, 10 elements to be exact. Only 3 of the elements are collapsable. I've read the layouts and rendering part of the Rails documentation but I'm still very confused as to how to do it especially in having to assign the collapsable class to only 3 elements. Any advice is helpful, thanks in advance.
x.HTML.erb
<h4>
<a href="#">
<li><%= link_to category.name, catalogo_path(cat_id: category.id), remote: true %></li>
</a>
</h4>
<% category.subcategories.each do |cat| %>
<li><%= link_to cat.name, catalogo_path(cat_id: cat.id), remote: true %></li>
<% end %>
Partial
<h4>
<a href="#">
<li><%= link_to category.name, catalogo_path(cat_id: category.id), remote: true %></li>
</a>
</h4>
<% category.subcategories.each do |cat| %>
<li><%= link_to cat.name, catalogo_path(cat_id: cat.id), remote: true %></li>
<% end %>
The HTML accordion structure I want to use:
<div class="accordion" id="accordion2">
<div class="accordion-group">
<h4>
Angulos
</h4>
<h4>
Soleras
</h4>
<h4>
Semiflechas
</h4>
<h4>
Redondos
</h4>
<div class="accordion-heading">
<h4>
<a class="accordion-toggle" data-toggle="collapse" href="#collapseOne">Cuadrados</a>
</h4>
</div>
<div id="collapseOne" class="accordion-body collapse in">
Cuadrados Normales
Cuadrados Retorcidos
</div>
<h4>
Canal
</h4>
<h4>
Vigas IPR
</h4>
<h4>
Vigas IPS
</h4>
</div>
<div class="accordion-group">
<div class="accordion-heading">
<h4>
<a class="accordion-toggle" data-toggle="collapse" href="#collapseTwo">Placas</a>
</h4>
</div>
<div id="collapseTwo" class="accordion-body collapse">
Placa de Rollo<br>
Placa de Grado
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading">
<h4>
<a class="accordion-toggle" data-toggle="collapse" href="#collapseThree">Laminas</a>
</h4>
</div>
<div id="collapseThree" class="accordion-body collapse">
Laminas Calientes<br>
Laminas Frias
Laminas Antiderrapantes
</div>
</div>
</div>

If I understand your question correctly, then you want to use partials to generate the HTML you posted. Here's a suggestion.
I don't think you need an .accordion-group around each set that you want to be collapsable. You can just put everything in one.
Here's the outermost ERB template.
<div class="accordion" id="accordion2">
<div class="accordion-group">
<%= #categories.each do |category| %>
<% if category.subcategories.any? %>
<%= render partial: "category_with_sub", locals: {category: category} %>
<% else %>
<%= render partial: "category", locals: {category: category} %>
<% end %>
<% end %>
</div>
</div>
Partial _category_with_sub.html.erb
<div class="accordion-heading">
<h4>
<a href="#">
<li><%= link_to category.name, catalogo_path(cat_id: category.id), class: "accordion-toggle", "data-toggle" => "collapse", remote: true %></li>
</a>
</h4>
</div>
<div id="collapseOne" class="accordion-body collapse in">
<% category.subcategories.each do |cat| %>
<li><%= link_to cat.name, catalogo_path(cat_id: cat.id), class: "accordion-inner", remote: true %></li>
<% end %>
</div>
Partial _category.html.erb
<h4>
<a href="#">
<li><%= link_to category.name, catalogo_path(cat_id: category.id), remote: true %></li>
</a>
</h4>
One final suggestion. It looks like you are using an old version of bootstrap. You might want to consider upgrading to the latest version.
UPDATE
Edited categories to be #categories
Assuming your controller looks something like this.
class CategoriesController < ApplicationController
def index # or whatever action you want
#categories = Category.all # or whatever query you want to use
end
end

Related

Dropdown menu within a menu in NavBar for Bootstrap

I am trying to create a dropdown of sub-links in a Navbar dropdown menu. Here is my current progress. I am not able to introduce the toggle button and proper toggle mechanism. When I press on the テンプレート管理, the entire dropdown menu disappeared, and I have to click the navbar menu again to see the list. The expected result is in the screenshot, not sure if Bootstrap has such similar component to use.
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
</ul>
<div class="dropdown">
<a class="nav-link" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false" style="padding: 0.5rem 0">
<%= inline_svg_tag 'setting.svg' %>
</a>
<ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-end" aria-labelledby="dropdownMenu2" >
<li class="mt-2" >
<%= link_to hospital_manage_hospitals_path, class: "dropdown-item" do %>
<%= inline_svg_tag 'hospital.svg' %> 医療機関管理
<% end %>
</li>
<li class="mt-2">
<%= link_to hospital_manage_hospital_admin_contacts_path, class: "dropdown-item" do %>
<%= inline_svg_tag 'logout.svg' %> 担当者管理
<% end %>
</li>
<li class="mt-2">
<%= link_to hospital_manage_referral_companies_path, class: "dropdown-item" do %>
<%= inline_svg_tag 'handshake.svg' %> 人材紹介会社管理
<% end %>
</li>
<li class="mt-2" data-bs-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample" >
<%= link_to hospital_manage_referral_companies_path, class: "dropdown-item" do %>
<%= inline_svg_tag 'template.svg' %> テンプレート管理
<% end %>
</li>
<div class="collapse" id="collapseExample">
<ul>
<%= link_to hospital_manage_spot_job_templates_path, class: "dropdown-item mt-2" do %>
スポット求人
<% end %>
<%= link_to hospital_manage_spot_job_templates_path, class: "dropdown-item mt-2" do %>
常勤・非常勤求人
<% end %>
<%= link_to hospital_manage_spot_job_templates_path, class: "dropdown-item mt-2" do %>
勤務票
<% end %>
<%= link_to hospital_manage_spot_job_templates_path, class: "dropdown-item mt-2" do %>
メッセージ
<% end %>
</ul>
</div>
</ul>
</div>

Haiving a problem to Paginate an iteration with Kaminari

I'm trying to paginate a result of an iteration using Kaminari. The pagination is showing well but its not cutting it into another page. All is showing on the same page even when I click on page 2, it still the same result as page 1
I'm using rails 6 and ruby 2.5
in my controller i have this
def dashboard
user_gigs = current_user.gigs
#user_gigs_pager = Kaminari.paginate_array(user_gigs).page(params[:page]).per(4)
end
then in my view I call the paginate
<div class="card">
<div class="card-header-title is-centered">
<%= paginate #user_gigs_pager %>
</div>
</div>
this my full view code
<% current_user.gigs.each do |gig| %>
<%# <% user_gigs = current_user.gigs %>
<div class="column is-one-third">
<div class="card">
<div class="card-image">
<%= link_to edit_gig_path(gig) do %>
<figure class="image is-4by3">
<!-- Gig Cover Image Here -->
<%= image_tag gig_cover(gig) %>
</figure>
<% end %>
</div>
<div class="card-content p-t-5 p-b-5">
<p class="subtitle is-6 m-b-5"><%= link_to gig.title.truncate(20), gig_path(gig) %></p>
<span class="star-review"><i class="fa fa-star"></i>
<%= gig.average_rating %> <span class="star-review">(<%= gig.reviews.count %>)</span>
</span>
</div>
<footer class="card-footer">
<p class="deletegig">
<%= link_to destroy_gig_path(gig), method: :delete, data: { confirm: "are you sure"} do %>
<span class="icon has-text-danger">
<i class="far fa-trash-alt"></i>
</span>
<% end %>
</p>
<% basic_price = gig.pricings.find{ |p| p.pricing_type == 'basic' }%>
<a class="has-text-danger is-block card-footer-item has-text-right">
<% if !basic_price.nil? %>
<span class="small-title">Points</span> <i class="fab fa-bitcoin"></i>
<strong><%= basic_price.price %></strong>
<% else %>
<strong>
<span class="small-title has-text-dark">Pas de point</span>
</strong>
<% end %>
</a>
</footer>
</div>
</div>
<% end %>
</div>
</div>
</div>
<div class="card">
<div class="card-header-title is-centered">
<%= paginate #user_gigs_pager %>
</div>
</div>
Kaminari.paginate_array(user_gigs).page(params[:page]).per(4)
expects page number as argument of page and number of records per page as argument of per.
Your params must contain page number (i.e. 1 or 2 or 3 ...) in :page key.
The solution was to apply the pagination on the ActiveRecord association instead of the array.
So in my controller it should be
#user_gigs_pager = current_user.gigs.page(params[:page]).per(4)
then in the view
<% #user_gigs_pager.each do |gig| %>
....
<%= paginate #user_gigs_pager %>

Best way to show empty lists in Rails

I have been working to follow the best practices in Rails in order to save time in the future.
I am displaying all terms in a view which has more than 0 results.
<% #terms.each do |t| %>
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{t.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, t) %></span>
</div>
</li>
<% end %>
What is the best way to display a different view if the term count is zero? I could do something like a simple if statement in the view doing a count but that seems cluttered. Any suggestions?
How about this:
<% #terms.each do |t| %>
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{t.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, t) %></span>
</div>
</li>
<% end.empty? and begin %>
<li class="media">
<!-- nothing to see -->
</li>
<% end %>
You can also move the code to a partial.
<%= render(:partial => 'terms', :collection => #terms) || 'There are no terms' %>
The partial:
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{term.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, term) %></span>
</div>
</li>
</li>

Dynamically Render Database Records in Rails using Bootstrap Tabs

I'm hoping to be able to render a tab for each of my project tasks. Currently, the only tab active is the first one (which I'm not terribly surprised since I'm including my index == 0 condition on the tabpanel div, but in my tinkering I haven't been able to select the proper tab) How can I change this to render the proper project task when selected? Is there an effective way to render this kind of content without using AJAX?
<div class='well col-xs-8 col-xs-offset-2'>
<ul class="nav nav-pills" role="tablist" id="myTabs">
<% #project.tasks.each_with_index do |task, index| %>
<li role="presentation" class="tab-pane <%= 'active' if index == 0 %>">
<a href="#<% task.title %>" aria-controls="<% task.title.downcase %>"
role="tab" data-toggle="tab"><%= task.title %></a>
</li>
<% end %>
</ul>
<div class="tab-content">
<% #project.tasks.each_with_index do |task, index| %>
<div role="tabpanel" class="tab-pane fade in <%= 'active' if index == 0 %>" id="<%= task.title.downcase %>">
<p><%= task.title %></p>
</div>
<% end %>
</div>
<%= javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" %>
<%= javascript_include_tag "//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js" %>
</div>
In this case, the tab body should read 'Support'
I would like to suggest using id with index number as shown in code below I merge the id with rails code <%=#{index}%>
below is some code sample that I created based your code above
<div class="panel with-nav-tabs panel-info">
<div class="panel-heading">
<ul class="nav nav-tabs">
<% #project.tasks.each_with_index do |task, index| %>
<% if index == 0 %>
<li class="active"><p><%= task.title %></p></li>
<% else %>
<li><%= task.title %></li>
<% end %>
<% end %>
</ul>
</div>
<div class="panel-body">
<div class="tab-content">
<% #project.tasks.each_with_index do |task, index| %>
<% if index == 0 %>
<div class="tab-pane fade in active" id="tab<%=#{index}%>">
<p><%= task.title %></p>
</div>
<% else %>
<div class="tab-pane fade" id="tab<%=#{index}%>">
<p><%= task.title %></p>
</div>
<% end %>
<% end %>
</div>
</div>

How to filter results by clicking on checkbox in ruby on rails

I am a newbie in ruby on rails it's also my first ruby application.
I want to filter results by clicking on checkbox which is coming from
the database and showing on the right side of my webpage, the checkbox
is lying on the left side. In the below I attached my webpage's
screenshot for easy understanding.
Anyone can help me, please, how can I solve this issue?
My Controller file code:resumes_controller.rb
class ResumesController < ApplicationController
before_filter :recruiter!
def resumes
#resume = Jobseeker.paginate(page: params[:page], :per_page => 10).order('jobseeker_id DESC')
end
end
My view file code: resumes.html.erb
<%= form_for :jobseekers,url: resumes_path(#jobseekers), action: :update, method: :post do |f| %>
<div>
<label for="Title">Education</label>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Masters
</div>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Bachelor
</div>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Associates
</div>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Diploma
</div>
More
</div>
<% end %>
<!-- item1 -->
<% #resume.each do | res| %>
<div class="job-item bio-card">
<div class="employer-information">
<div class="col-sm-6">
<ul class="list-unstyled">
<li class="employ-name">
<a href="<%= view_profile_path(:jobseeker_id => res.jobseeker_id) %>">
<%= res.first_name %> <%= res.last_name %></a> <span>- <%= res.current_address %></span>
</li>
<li><%= res.institute_name %></li>
</ul>
</div>
<% if res.attach_resume.nil? %>
<div class="col-sm-6 employ-pdf-box">
<div class="employ-pdf">
<i class="fa fa-file-powerpoint-o icon"></i>
<h3>Empty</h3>
</div>
</div>
<% else %>
<div class="col-sm-6 employ-pdf-box">
<a href="<%= res.attach_resume.resume %>"target="_blank">
<div class="employ-pdf">
<i class="fa fa-file-powerpoint-o icon"></i>
<h3><%= res.first_name %> <%= res.last_name %>.pdf</h3>
</div>
</a>
</div>
<% end %>
</div>
</div>
<% end %>
<!-- End item1 -->

Resources