How to filter results by clicking on checkbox in ruby on rails - 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 -->

Related

How can I rearrange my params to get the desired structure?

I am on a rails 5.07 app. I have some params coming in from a form and that forms partial. I need to restructure these params or re-wire my model associations or try to implement Nested Forms...WHATEVER incantation I need to do in order to restructure / reorder the params from...
Note: I'm not sure how I can be more specific or ask to do only one thing here. As I am asking to do "only one thing"....sooo before you close my question....please be specific as to why? and give an example of how I can ask...this one thing... to rearrange params, differently?
current params
> {"utf8"=>"✓",
> "assignment"=>{"volunteer_shift_attributes"=>{"volunteer_task_type_id"=>"41",
> "roster_id"=>"7", "program_id"=>"9", "set_description"=>"Nov10",
> "set_date"=>"2021-01-08"}, "start_time(1i)"=>"2021",
> "start_time(2i)"=>"11", "start_time(3i)"=>"10",
> "start_time(4i)"=>"08", "start_time(5i)"=>"00",
> "end_time(1i)"=>"2021", "end_time(2i)"=>"11", "end_time(3i)"=>"10",
> "end_time(4i)"=>"09", "end_time(5i)"=>"00", "notes"=>"Nov10",
> "contact_id"=>"166574", "closed"=>"0", "lock_version"=>"0"},
> "contact_element_prefix"=>"contact", "commit"=>"Submit",
> "controller"=>"volunteer_events", "action"=>"create_shift"}
to Desired params structure
{"assignment"=>{"volunteer_shift_attributes"=>
{"volunteer_task_type_id"=>"41", "roster_id"=>"7", "program_id"=>"9",
"set_description"=>"fonso says wtf?"}, "set_date"=>"2021-01-07",
"contact_id"=>"166574", "closed"=>"0", "start_time(4i)"=>"12",
"start_time(5i)"=>"00", "end_time(4i)"=>"13", "end_time(5i)"=>"00",
"notes"=>"fuuuuuuuuuk"}, "lock_versions"=>["0"],
"contact_element_prefix"=>"contact", "commit"=>"Update",
"controller"=>"volunteer_events", "action"=>"create_shift"}
so I can say something like...
params["assignment"]["volunteer_shift_attributes"]["roster_id"]
and result being "7"
Please let me know if I need to include my model associations or anything else. Happy to oblige.
UPDATE:
per request, adding the form and the partial below...
here is the main form that produces the undesired outcome
<!--This is NEW action form-->
<%= form_for #assignment, :url => #my_url, remote: true do |f| %>
<!-- #FIXME need a fields_for 4 the volunteer_event-->
<div class="">
<div class="modal-body d-flex">
<div class="col-sm-8 border-right">
<section id="location-date-time-notes" class="flex">
<% if #assignment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#assignment.errors.count, "error") %> prohibited this assignment from being saved:</h2>
<ul>
<% #assignment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<!--VOLUNTEER SHIFT-->
<!--TODO: make this a partial under field_for-->
<%= f.fields_for :volunteer_shift do |builder| %>
<%= render 'assignments/volunteer_shift_fields', vs: builder %>
<% end %>
<!--TODO: Volunteer Shift end -->
<div id="time-row" class="d-flex flex-row">
<label for="assignment_time" class="col-sm-3 p-2">
<i class="fa fa-clock-o fa-lg" aria-hidden="true"></i> Time:
</label>
<div class="col- p-2">
<div class="myStartTime" id="start_time_<%= #assignment.id %>">
<%= f.time_select :start_time %>
</div>
</div>
<div class="col- p-2"><i class="fa fa-arrows-h fa-lg" aria-hidden="true"></i></div>
<div class="col-sm-3 p-2">
<div class="myEndTime" id="end_time_<%= #assignment.id %>">
<%= f.time_select :end_time %>
</div>
</div>
</div>
<div class="d-flex flex-row">
<label for="assignment_notes" class="col-sm-3 p-2">
<i class="fa fa-clipboard fa-lg" aria-hidden="true"></i> Notes:
</label>
<div class="col-sm-9 p-2">
<div class="d-flex flex-row">
<span> Notes only get saved if a contact is assigned the shift, and get removed when the contact is removed from the shift.</span>
<div class="">
<%= f.label :notes %>
<%= f.text_area :notes %>
</div>
</div>
</div>
</div>
</section>
</div>
<div class="col-sm-4">
<!-- Contact Section-->
<div id="contact_section">
<% if #assigned_contacts && #assigned_contacts.length > 0 %>
<h2>Previously Assigned Contacts</h2>
<% #assigned_contacts.each do |c| %>
<%= label_tag "assigned_contacts[#{c.id}]", "Are you sure you want to remove the currently scheduled volunteer, #{c.display_name} (##{c.id}), from the assignment(s)?" %>
<%= check_box_tag "assigned_contacts[#{c.id}]", "replace", #replaced_contacts.include?(c.id) %>
<% end %>
<% end %>
<input id="contact_element_prefix" name="contact_element_prefix" type="hidden" value="contact">
<div class="name large flex-row">
<%= f.label :contact_id %><%= f.text_field :contact_id %>
</div>
<div id="display-contact" class="d-flex flex-row">
<% if f.object.contact_id %>
<%= render partial: 'contacts/contact_display', locals: { contact:f.object.contact} %>
<% else %>
<div>no contact attatched- _form.html called</div>
<%#= link_to 'Show Contact', contact_path(f.object.contact_id), remote: true %>
<% end %>
</div>
<!-- FIXME: replace this logic stack with AJAX-->
<%#= contact_field("#obj", "contact_id",
:locals => {:options => {
:object_name => f.object_name.to_s,
:field_name => 'contact_id',
:on_display => 'display_disciplinary_notes(); display_contact_notes();'
}}
) %>
<%= f.label :closed, "Is this slot closed?" %>
<%= f.check_box :closed %>
<!--Contact Section END-->
<!--Attendance / Call Status start-->
<% if f.object.id && f.object.contact_id %>
<div class="flex-row">
<div class="col-25"><label for="assignment_attendance_type_id">Attendance:</label></div>
<div class="col-75"><%= select(f.object_name,
"attendance_type_id",
AttendanceType.all.sort_by(&:id).collect {|p| [ p.name, p.id ] },
:include_blank => true) %></div>
</div>
<div class="flex-row">
<div class="col-25"><label for="assignment_call_status_type_id">Call status:</label></div>
<div class="col-75"><%= select(f.object_name,
"call_status_type_id",
([["not called yet", ""]] + CallStatusType.all.sort_by(&:id).collect {|p| [ p.name, p.id ] }),
:include_blank => false) %></div>
</div>
<% end %>
<!-- Attendance / Call Status End-->
<!-- LOCK VERSION-->
<div class="flex-row">
<div class="col-25">
<%= f.label :lock_version %>
</div>
<div class="col-75">
<%= f.number_field :lock_version %>
</div>
</div>
<!-- LOCK end-->
</div>
</div>
</div>
<div class="modal-footer d-flex justify-content-between">
<div class="edit_icons d-flex flex-row">
<div class="d-flex flex-column">
<!-- <i class="fa fa-share-alt-square fa-lg" aria-hidden="true"></i> Split-->
<!-- <i class="fa fa-files-o fa-lg" aria-hidden="true"></i> Copy-->
</div>
<div class="d-flex flex-column">
<%#= link_to '<i class="fa fa-pencil-square-o fa-lg" aria-hidden="true"></i>Edit'.html_safe, edit_assignment_path, remote: true%>
<!-- <i class="fa fa-refresh fa-lg" aria-hidden="true"></i> Reassign-->
</div>
</div>
<div>
<button type="button" class="btn btn-secondary mr-2" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary"><%= f.submit "Submit" %></button>
<!-- <input id="assignment_submit" name="commit" type="submit" value="Update">-->
</div>
</div>
</div>
<% end %>
and the partial file _volunteer_shift_fields
<!--<div class="name large flex-row">-->
<%= vs.label :volunteer_shift %>
<!--</div>-->
<div id="volunteer_shift" class="d-flex flex-row">
<div class="col-sm-12 p-2">
<div id="volunteer_shift" class="text-right">
<div class="field">
<%= vs.label :volunteer_task_type_id %>
<%= vs.select 'volunteer_task_type_id', options_from_collection_for_select([VolunteerTaskType.new(:description => ""), VolunteerTaskType.instantiables.effective_on(Date.today)].flatten, "id", "description") %>
</div>
<div class="field">
<%= vs.label :roster_id %>
<%= vs.select 'roster_id', options_from_collection_for_select([Roster.new(:name => ""), Roster.all].flatten, "id", "name") %>
</div>
<div class="field">
<%= vs.label :program_id %>
<%= vs.select 'program_id', options_from_collection_for_select([Program.new(:name => ""), Program.where(:volunteer => true)].flatten, "id", "name")%>
</div>
<div class="field">
<%= vs.label :set_description, "Description" %>
<%= vs.text_area(:set_description) %>
</div>
<div class="field">
<%= vs.label :set_date, "Date" %>
<%= vs.text_field(:set_date) %>
</div>
</div>
</div>
</div>
UPDATE 2:
Big thanks to sam. adjusting my partial has gotten me 90% there. I "think" I just need to move the remaining form elements from parent to child and vice versa to get the rest of the way there. I'll post the outcome once I've tried that as well.
in your _volunteer_shift_fields file, try to bind your form element with the form object. Replace select_tag with vs.select, text_field with vs.text_field. Then Rails will automatically build the desired params.

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

Rails: Will paginate with current_user

I'm currently trying to implement will_paginate into my application, the problem that I'm currently facing is that I want to display it to different users, as users can have many wines and orders.
I'm looking for something to combine the current_user.wines with Wine.paginate..
I currently don't have any idea how to implement this
My wine controller:
def index
#wines = current_user.wines
#wines = Wine.paginate(page: params[:page])
end
As you can see I'm currently overriding #wines...
My wine model:
class Wine < ApplicationRecord
enum instant: {Reservieren: 0, Sofort: 1}
belongs_to :user
self.per_page = 1
end
My index view:
<div class="container-small">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
Deine Weine
</div>
<div class="panel-body">
<% if !current_user.admin? %>
<% current_user.favorites.each do |favorite| %>
<div class="row">
<div class="col-md-2">
<%= image_tag favorite.wine.cover_photo(:thumb) %>
</div>
<div class="col-md-7">
<h4><%= favorite.wine.wine_name %></h4>
</div>
<div class="col-md-3 right">
<%= link_to "Bestellen", wine_path(#wines), class: "btn btn-form" %>
</div>
</div>
<hr/>
<% end %>
<% else %>
<% #wines.each do |wine| %>
<div class="row">
<div class="col-md-2">
<%= image_tag wine.cover_photo(:thumb) %>
</div>
<div class="col-md-7">
<h4><%= wine.wine_name %></h4>
</div>
<div class="col-md-3 right">
<%= link_to "Bearbeiten", details_wine_path(wine), class: "btn btn-form" %>
</div>
</div>
<hr/>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
</div>
<%= will_paginate(#wines) %>
UPDATE
Now it's working in the wine controller, but when I want to implement it into the reservations controller, into the your_orders and your_reservations methods, it's not showing up in the view. I'm not getting an error, when I call <%= will_paginate(#wines) %>
Reservations view:
<div class="row" id="orders">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
Alle Bestellungen
</div>
<div class="panel-body">
<% #wines.each do |wine| %>
<% wine.reservations.each do |reservation| %>
<div class="row">
<% if reservation.Bearbeitung? %>
<div class="col-md-3">
Reserviert am<br/>
<%= reservation.start_date.strftime('%v') %>
</div>
<% else %>
<div class="col-md-3">
Erwartet am<br/>
<%= reservation.start_date.strftime('%v') %>
</div>
<% end %>
<div class="col-md-2">
<p><%= reservation.status %></p>
<div class="form-inline">
<% if reservation.Bearbeitung? %>
<%= link_to approve_reservation_path(reservation), method: :post do %> <i class="fa fa-thumbs-up fa-lg"></i> <% end %> |
<%= link_to decline_reservation_path(reservation), method: :post do %> <i class="fa fa-thumbs-down fa-lg"></i> <% end %>
<% end %>
</div>
</div>
<div class="col-md-4">
<%= reservation.bottle %>x
<%= link_to reservation.wine.wine_name, wine_path(reservation.wine) %><br/>
Gesamt: <%= reservation.total %>€
</div>
<div class="col-md-3 pull-right">
Lieferung an<br/><br/>
<span>
<%= link_to user_path(reservation.user) do %>
<%= reservation.user.fullname %><br/>
<% end %>
<%= reservation.user.location %>
</span>
</div>
</div><!-- row -->
<hr/>
<% end %>
<% end %>
</div><!-- panel body -->
</div><!-- panel default -->
</div><!-- col md 12 -->
</div><!-- row -->
</div><!-- row -->
</div><!-- container -->
<%= will_paginate(#wines) %>
Reservations Controller:
def your_orders
#orders = current_user.reservations.order(start_date: :asc)
end
def your_reservations
#wines = current_user.wines.paginate(page: params[:page])
redirect_to root_path unless current_user.admin == true
#count = Reservation.count(:all)
#total = Reservation.sum(:total)
end

Trying to link reviews users actively input under the show page of the specific movie for which the review was written

Under app->controllers->reviews_controller.rb, I have the following code:
`def create
review = Review.new
review.movie_id = params["movie_id"]
review.rating = params["rating"]
review.content = params["content"]
review.save
redirect_to "/reviews"
end
I think that I need to use this information to link a review a user makes in real time to the view page for the movie (just basically building a mini IMDB page with about 12 movies currently)app->views->movies->show.html.erb.
<h2>Reviews</h2>
Do I need to use a params hash with "redirect_to" for the params["content"] above?
I hope this post is helpful to others seeking to gather user data and show in real time on the correct page.
This is reviews->index.
<h1>Reviews</h1>
<% for review in Review.order('created_at desc') %>
<% movie = Movie.find_by(id: review.movie_id) %>
<% if movie != nil %>
<div class="row mb-3">
<div class="col-sm-12">
<div class="card">
<div class="card-body">
<h5 class="card-title">
<% 1.times do %>
<p>Howdy!</p>
<% end %>
★ ★ ★ ★ ★ ★
<p>
<%#= review.updated_at.strftime("%B %d, %Y %l:%M %P") %>
<%= time_ago_in_words(review.updated_at) %> ago.
</p>
<h6 class="card-subtitle mb-2 text-muted">
<%= movie.title %>
</h6>
</h5>
<p class="card-text">
<%= review.content %>
</p>
</div>
</div>
</div>
</div>
<% end %>
And this is the movies->show.
<% movie_id = params[:id] %>
<% movie = Movie.find_by(id: movie_id) %>
<div class="row">
<div class="col-sm-6">
<h2>
<%= movie.title %>
</h2>
</div>
</div>
<div class="row">
<div class="col-sm-3">
<img src="<%= movie.poster_url %>" class="img-fluid">
</div>
<div class="col-sm-6">
<a href="/movies/<%= movie.id %>/edit" class="float-right">
<i class="fas fa-pen-square"></i>
Edit This Movie
</a>
<% director = Director.find_by(id: movie.director_id) %>
<% if director != nil %>
<p>
Directed by <%= director.name %>
</p>
<% end %>
<p class="lead">
<strong><%= movie.plot %></strong>
</p>
<div class="row mt-3">
<% characters_in_this_movie = Character.where(movie_id: movie.id) %>
<% characters_in_this_movie.each do |character| %>
<% the_actor = Actor.find_by(id: character.actor_id) %>
<div class="col-sm-3 text-center">
<%= image_tag(the_actor.photo_url, class: 'img-fluid mb-3') %>
<br>
<strong><%= the_actor.name %></strong>
<br>
as <%= character.name %>
</div>
<% end %>
</div>
</div>
</div>
<div class="row mt-5 justify-content-center">
<div class="col-sm-6">
<a href="/reviews/new?movie_id=<%= movie.id %>" class="float- right">
<i class="fas fa-pen-square"></i>
Write a Review
</a>
<h2>Reviews</h2>
</div>
</div>

Rails loop, link_to model_view

Rails: Need help looping through model array to link to the show page. I want to show the name, but link to the path. Seems like it should be simple but I have been coding all night and my brain is fried! please help.
<div class="container">
<div class="row">
<% #bars.each do |bar| %>
<div class="col-xs-6 something">
<div class="firstBar">
<%= link_to bars_path %>
<% end %>
</div>
</div>
</div>
</div>
This should work:
<div class="container">
<div class="row">
<% #bars.each do |bar| %>
<div class="col-xs-6 something">
<div class="firstBar">
<%= link_to bar.name, bar %>
</div>
</div>
<% end %>
</div>
</div>
You could also do <%= link_to bar.name, bars_path(bar) %>, but is prettier to just give the object. Rails will know which Url helper to use given a specific object.
Take a look at the UrlHelper documentation
Try this
<div class="container">
<div class="row">
<% #bars.each do |bar| %>
<div class="col-xs-6 something">
<div class="firstBar">
<%= link_to bar.name, bar_path(bar) %>
</div>
</div>
<% end %>
</div>
</div>

Resources