Best way to show empty lists in Rails - ruby-on-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>

Related

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

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>

Rails - Inserting select elements from controller in partial

(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

How to avoid same instance variables for footer

I have a footer in my rails application that shows all recent posts and categories produced by two instances variables in the welcome_controller.rb
But then if i go to another section that has a different controller responsible i get an error as the #posts and #categories instance variables are not there to loop through and list the latest posts and categories.
welcome_controller.rb (code extract)
#categories = Category.all
#posts = Post.order("created_at DESC")
_footer.html
<div class="row footer-black">
<div class="large-3 columns text-left">
<h4>Categories</h4>
<ul>
<% #categories.each do |c| %>
<% unless c.header? %>
<% if c.restriction %>
<% if check_for_free_questions_in_restricted_category(c) %>
<li> <%= link_to c.title, category_random_free_question_path(c),'data-no-turbolink' => true %> </li>
<% else %>
<li> <%= link_to c.title, new_subscription_path,'data-no-turbolink' => true %> </li>
<% end %>
<% else %>
<li> <%= link_to c.title, category_random_question_path(c),'data-no-turbolink' => true %> </li>
<% end %>
<% end %>
<% end %>
</ul>
</div>
<div class="large-5 columns text-left">
<h4>Latest Posts</h4>
<ul>
<% #posts.each do |p| %>
<li> <%= link_to truncate(p.title, length:70), blog_post_path(p) %> </li>
<% end %>
</ul>
</div>
<div class="large-4 columns text-left social-links">
<h4>Social</h4>
<a href="#">
<i class="fa fa-facebook-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-twitter-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-google-plus-square fa-2x"></i>
</a>
</div>
</div>
is there a way to show categories and posts in footer without instantiating same variables over and over again?
The short answer is 'no'. Every variable must be instantiated (initialized) before it can be used (in this case, in your view).
Now, there are some more efficient ways of instantiating these variables than writing:
#categories = Category.all
#posts = Post.order("created_at DESC")
in multiple controllers. But, that's a different question. If you're interested in that, then ask a new question or refine this one.

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