Using ransack for Rails application material dropdown select in view - ruby-on-rails

Just need to know how you add ransack code in rails view as I m taking array list as category items and passing into the bootstrap material
the code for rails
on top of starting line search path code
<%= search_form_for #search, url: events_path do |f| %>
dropdown code dropdown without ransack which gives output with correct dropdown list
<div class="col">
<div class="dropdown" id="dropdown">
<p class="selected" id="selected"><span>Filter By Category1</span> <i class="material-icons">keyboard_arrow_down</i></p>
<ul class="dropdown-list" id="dropdown-list">
<a href="/events">
<li>All Events </li>
<% #category_list.each do |cat| %>
<li class="selected" id="list"> <%= cat.name %></li>
<% end %>
</ul>
</div>
</div>
I m trying to add ransack this does not display my list
<div class="col">
<div class="dropdown" id="dropdown">
<%= f.select :category_name_eq, class: "selected" %>
<ul class="dropdown-list" id="dropdown-list">
<a href="/events">
<li>All Events </li>
<% #category_list.each do |cat| %>
<li class="selected" id="list"> <%= cat.name %></li>
<% end %>
</ul>
</div>
</div>

you can do with list like this below, in case you allow blank option you should add include_blank: true as sample below
<div class="row form-group">
<%= f.label "Filter By Category1 <i class="mi md-18">arrow_downward</i>".html_safe, :class => 'col-form-label col-sm-3' %>
<div class="col-sm-9">
<%= f.select :category_name_eq,
#category_list.all.map { |cat| [cat.name, cat.name] },
{ include_blank: true }, { class: 'form-control' } %>
</div>
</div>

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

Get from the link_to an array of values Ruby on Rails

<i class="dropdown icon"></i>
<div class="menu">
<% #dogs.each do |dog| %>
<%= link_to dog[:name], params.merge(dog_id: dog[:id], page: nil), class: 'item' %>
<% end %>
</div>
</div>
I'm working on a project, I need to add to the params by clicking the id array with one key. Tried like this, but leads to unexpected results, I ask for help.
<i class="dropdown icon"></i>
<div class="menu">
<% #dogs.each do |dog| %>
<%= link_to dog[:name], params.merge(dog_id: params.push(dog[:id]), page: nil), class: 'item' %>
<% end %>
</div>
</div>
I understand what should look like this
<%= link_to dog[:name], params.merge(dog_id: [0, 1, 4, 6], page: nil), class: 'item' %>
But I can not realize. Thanks!

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

Rails: How should I adjust the 'each' method under a nest structure?

Before putting pinunder the nest structure of group,
One of the view looked like this:
<div class="transitions-enabled" id="pins">
<% #pins.each do |pin| %>
<div class="box panel panel-default">
<%= link_to (image_tag pin.image.url), pin %>
<div class="panel-body">
<h2><%= link_to pin.title, pin %></h2>
</div>
<div class="footer">
<div class="row footer-all">
<div class="col-md-6">
<p class="user">Submitted by <%= pin.user.username %></p>
</div>
<div class="col-md-6">
<div class="btn-group like_btn">
<% if user_signed_in? %>
<% if current_user.voted_up_on?(pin) %>
<%= link_to unlike_pin_path(pin), method: :put, class: "btn btn-default" do %>
<span class='glyphicon glyphicon-heart'></span>
<%= pin.get_upvotes.size %>
<%end%>
<% else %>
<%= link_to like_pin_path(pin), method: :put, class: "btn btn-default" do %>
<span class='glyphicon glyphicon-heart'></span>
<%= pin.get_upvotes.size %>
<%end%>
<% end %>
<% else %>
<div class="like_btn">
<span class='glyphicon glyphicon-heart'></span>
<%= pin.get_upvotes.size %>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
<%end%>
How should I adjust it after I put it under group (I'll mark the code which I think needed to change with'* *'):
Group has_many pins
Pin belongs_to group
<div class="transitions-enabled" id="pins">
<% #pins.each do |pin| %>
<div class="box panel panel-default">
<%= link_to (image_tag pin.image.url), *pin* %>
<div class="panel-body">
<h2><%= link_to pin.title, *pin* %></h2>
</div>
<div class="footer">
<div class="row footer-all">
<div class="col-md-6">
<p class="user">Submitted by <%= pin.user.username %></p>
</div>
<div class="col-md-6">
<div class="btn-group like_btn">
<% if user_signed_in? %>
<% if current_user.voted_up_on?(*pin*) %>
<%= link_to unlike_group_pin_path(*pin*), method: :put, class: "btn btn-default" do %>
<span class='glyphicon glyphicon-heart'></span>
<%= pin.get_upvotes.size %>
<%end%>
<% else %>
<%= link_to like_group_pin_path(*pin*), method: :put, class: "btn btn-default" do %>
<span class='glyphicon glyphicon-heart'></span>
<%= pin.get_upvotes.size %>
<%end%>
<% end %>
<% else %>
<div class="like_btn">
<span class='glyphicon glyphicon-heart'></span>
<%= pin.get_upvotes.size %>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
<%end%>
After stating that your objective is to use this view file to filter the pins depending on its group, then I can think of two approaches for this.
Use nested resources. (RailsCast)
This is a little trickier than 2. but it is much more organised, and RESTful
You could check the RailsCast if you are interested. Otherwise, I will just go explaining 2. as it's simpler.
Use params[:group_id] to filter the pins.
In the PinsController, add the following at the very end of the method where this view is rendered.
#group = Group.where(id: params[:group_id]).first
#pins = #pins.where(group: #group) if #group
Then in your view files, you'll just have to tweak your links to specify the group_id parameter. If this view file is associated to the #index method, then change
<%= link_to 'Link', pins_path %>
Into
<%= link_to 'Link', pins_path(group_id: 1) %>
Change 1 accordingly as you wish. If you don't specify the parameter group_id, the link will still work but you will notice that the page will show all pins, and not filtered by group.
Thanks for everyone's help, I think I find the problem:
The view should be changed into:
<div class="transitions-enabled" id="pins">
<% #pins.each do |pin| %>
<div class="box panel panel-default">
<%= link_to (image_tag pin.image.url), group_pin_path(#group, pin)
%>
<div class="panel-body">
<h2><%= link_to pin.title, group_pin_path(#group, pin) %></h2>
</div>
<div class="footer">
<div class="row footer-all">
<div class="col-md-6">
<p class="user">Submitted by <%= pin.user.username %></p>
</div>
<div class="col-md-6">
<div class="btn-group like_btn">
<% if user_signed_in? %>
<% if current_user.voted_up_on?(#group, pin) %>
<%= link_to unlike_group_pin_path(#group, pin), method: :put, class: "btn btn-default" do %>
<span class='glyphicon glyphicon-heart'></span>
<%= pin.get_upvotes.size %>
<%end%>
<% else %>
<%= link_to like_group_pin_path(#group, pin), method: :put, class: "btn btn-default" do %>
<span class='glyphicon glyphicon-heart'></span>
<%= pin.get_upvotes.size %>
<%end%>
<% end %>
<% else %>
<div class="like_btn">
<span class='glyphicon glyphicon-heart'></span>
<%= pin.get_upvotes.size %>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
<%end%>
And remember to state #group = Group.find(params[:group_id]) in the PinController, this is very important.

How do I detect the first iteration within my view? (Rails 4)

In my view I have:
<div class="item active">
<%= image_tag "2.jpg",size: "500x300", class: "no-resize" %>
<div class="carousel-caption">
Some tag.
</div>
</div>
<% #photos.each do |photo|%>
<div class="item">
<%= image_tag photo.attachment.url,size: "500x300",class: "no-resize" %>
<div class="carousel-caption">
Some tag.
</div>
</div>
<% end %>
Is there DRYer way of accomplishing this (as the only change I make is adding the class 'active' for the first photo) i.e. can I put the first item within the loop and detect the first iteration?
Try each_with_index
<% #photos.each_with_index do |photo, i |%>
<div class="item <%= "active" if i.zero? %>">
<%= image_tag photo.attachment.url,size: "500x300",class: "no-resize" %>
<div class="carousel-caption">
Some tag.
</div>
</div>
<% end %>
['apple', 'orange', 'pear'].each_with_index do |fruit, index|
p "first is #{fruit}" if index == 0
p fruit
end

Resources