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
Related
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>
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 -->
Here is part of my index.html
<div class="col-md-8">
<% #books.each do |book| %>
<div class="row">
<div class="col-md-4">
<%= image_from_amazon(book.amazon_id) %>
</div>
<div class="col-md-8">
<h3 class = "text-info">
<%= book.title %>
</h3>
<br>
<em class ="text-muted">
written by <%= book.author %>
</em>
<br>
<br>
<p>
<%= book.description %>
</p>
<% book.genres.each do |genres| %>
<span class="label label-primary">
<%= genres.name %>
</span>
 
<% end %>
</div>
</div>
<% end %>
</div>
Basically, it will display 3 books, and it works fine.
Then, I move that code into _book.html.erb and edit above code into
<%= render #books %>
However, it repeat 3 times, that is, it display 9 books. And the sequence is [1st,2nd,3rd] [1st,2nd,3rd] [1st,2nd,3rd] like this picture.
Update
update index.html.erb
<div class="clearfix">
<div class="col-md-12">
<%= render #books %>
<div class="col-md-4">
<h3>Genre (Click to filter books)</h3>
<br>
<li>
<span class = 'label label-danger'>
<%= link_to "No filter" ,books_path, style: 'color:#FFFFFD' %>
</span>
<br>
<br>
</li>
<% #genres.each do |genres| %>
<li>
<span class = 'label label-primary' style="color:#FFFFFD">
<%= link_to genres.name ,books_path(filter: genres.name),style: 'color:#FFFFFD' %>
</span>
</li>
<br>
<% end %>
</div>
</div>
<!-- clearfix -->
</div>
Try the following:
<div class="col-md-8">
<%= render #books %>
</div>
And make sure <% #books.each do |book| %> and its <% end %> tag aren't in the partial.
Rendering Collections Docs
Edit
#index.html.erb
<div class="col-md-8">
<%= render #books %>
</div>
# _book.html.erb
<div class="row">
<div class="col-md-4">
<%= image_from_amazon(book.amazon_id) %>
</div>
<div class="col-md-8">
<h3 class = "text-info">
<%= book.title %>
</h3>
<br>
<em class ="text-muted">
written by <%= book.author %>
</em>
<br>
<br>
<p>
<%= book.description %>
</p>
<% book.genres.each do |genres| %>
<span class="label label-primary">
<%= genres.name %>
</span>
 
<% end %>
</div>
</div>
I'm trying to get two different types of tags working using the acts_as_taggable_on gem. I was following this link. I'm getting this error:
syntax error, unexpected keyword_ensure, expecting keyword_end
yntax error, unexpected end-of-input, expecting keyword_end
The error says the problem is line 92 of my show.html.erb, but when I put and <% end %> there, it doesn't fix it.
show.html.erb
<%- title "#{#artwork.title} — a #{#artwork.category.downcase} by Peter Bentley"%>
<% if user_signed_in? %>
<div class="row">
<div class="col-md-12">
<%= link_to 'Edit', edit_artwork_path(#artwork) %> |
<%= link_to 'Artworks Table', admin_path %>
</div>
</div>
<% end %>
<div class="row">
<div class="col-md-8">
<%= image_tag(#artwork.image.url, :class => 'img-responsive') %>
</div>
<div class="col-xs-12 col-md-4">
<p class="field-label">Title</p>
<h1><%= #artwork.title %></h1>
</div>
<div class="col-xs-6 col-sm-3 col-md-4">
<p class="field-label">Medium</p>
<h3><%= #artwork.medium %></h3>
</div>
<% unless #artwork.date.blank? %>
<div class="col-xs-6 col-sm-3 col-md-2">
<p class="field-label">Date</p>
<h3><%= #artwork.date.strftime("%b %d %Y") %></h3>
</div>
<% end %>
<% unless #artwork.height.blank? | #artwork.width.blank?%>
<div class="col-xs-6 col-sm-3 col-md-4">
<p class="field-label">Size</p>
<h3><%= #artwork.height %> x <%= #artwork.width %> in.</h3>
</div>
<% end %>
<div class="col-xs-6 col-sm-3 col-md-2">
<p class="field-label">Genre</p>
<h3><%= #artwork.genre %></h3>
</div>
<div class="col-xs-6 col-sm-3 col-md-2">
<p class="field-label">Category</p>
<h3><%= #artwork.category %></h3>
</div>
<% unless #artwork.availability.blank? %>
<div class="col-xs-12 col-sm-3 col-md-4">
<% if #artwork.availability == 'Available for purchase' %>
<p class="field-label">Availability</p>
<h3><%= #artwork.availability %></h3>
<%= link_to "Add to Inquiry List", root_path, class: "btn btn-primary" %>
<% else %>
<p class="field-label">Availability</p>
<h3><%= #artwork.availability %></h3>
<% end %>
</div>
<% end %>
<% unless #artwork.series.blank? %>
<div class="col-xs-6 col-sm-6 col-md-4">
<p class="field-label">Series</p>
<% #artwork.series.each do |series| %>
<span class="tags">
<%= link_to series.name, series_url(:series => series.name) %>
</span>
</div>
<% end %>
<% unless #artwork.tags.blank? %>
<div class="col-xs-6 col-sm-6 col-md-4">
<p class="field-label">Tags</p>
<% #artwork.tags.each do |tag| %>
<span class="tags">
<%= link_to tag.name, tagged_url(:tag => tag.name) %>
</span>
</div>
<% end %>
</div> <!-- end of 'row' div -->
<div class="fixed-prev-next">
<div class="btn-group" role="group" aria-label="...">
<%= link_to "Previous", #artwork.previous, :class => "btn btn-default" if #artwork.previous.present? %>
<%= link_to "Next", #artwork.next, :class => "btn btn-default" if #artwork.next.present? %>
</div>
</div>
<% end %>
artworks_controller.erb has
def index
if params[:tag]
#artworks = Artwork.tagged_with(params[:tag])
else
#artworks = Artwork.all
end
end
def tagged
if params[:tag].present?
#artworks = Artwork.tagged_with(params[:tag])
else
#artworks = Artwork.all
end
end
def series
if params[:series].present?
#artworks = Artwork.tagged_with(params[:series])
else
#artworks = Artwork.all
end
end
and private method that permits
:tag_list, :tag, :series_list, :series
artwork.rb has
acts_as_taggable
acts_as_taggable_on :tags, :series
What am I doing wrong?
You are failing to close 2 of your loops with an <% end %>.
<% unless #artwork.series.blank? %>
<div class="col-xs-6 col-sm-6 col-md-4">
<p class="field-label">Series</p>
<% #artwork.series.each do |series| %>
<span class="tags">
<%= link_to series.name, series_url(:series => series.name) %>
</span>
<%# Need end here, as illustrated on the next line. %>
<% end %>
</div>
<% end %>
<% unless #artwork.tags.blank? %>
<div class="col-xs-6 col-sm-6 col-md-4">
<p class="field-label">Tags</p>
<% #artwork.tags.each do |tag| %>
<span class="tags">
<%= link_to tag.name, tagged_url(:tag => tag.name) %>
</span>
<%# Need end here, as illustrated on the next line. %>
<% end %>
</div>
<% end %>
Take more care about how you're indenting your code. It matters and helps you to more easily prevent and debug syntax problems like this.
The code below is fast becoming a common theme in my rails app. I have a bunch of conditions in my view for handling empty data, as well as, managing the push and pull of my grid. This will only grow as I begin to add the 3 other status's. My questions is this. What is the best way of managing my grid elegantly either in the view or controller so my views don't become increasingly bloated with conditions?
<% if #jobs.where(status: 'published').size == 0 %>
<div class="row">
<div class="large-12 columns">
</div>
</div>
<% end %>
<% if #jobs.where(status: 'published').size == 1 %>
<div class="row">
<div class="large-4 push-8 columns">
</div>
</div>
<% elsif #jobs.where(status: 'published').size == 2 %>
<div class="row">
<div class="large-4 push-4 columns">
</div>
</div>
<% else %>
<% #jobs.in_groups_of(3, false) do |row| %>
<div class="row">
<% for job in row %>
<div class="large-4 medium-4 columns">
</div>
<% end %>
</div>
<% end %>
<% end %>
I think you should use switch case statement for managing multiple conditions.
#status = #jobs.where(status: 'published').size
<% case #status %>
<% when 0 %>
<div class="row">
<div class="large-12 columns">
</div>
</div>
<% when 1 %>
<div class="row">
<div class="large-4 push-8 columns">
</div>
</div>
<% when 2 %>
<div class="row">
<div class="large-4 push-4 columns">
</div>
</div>
<% else %>
<% #published_jobs.in_groups_of(3, false) do |row| %>
<div class="row">
<% for job in row %>
<div class="large-4 medium-4 columns">
</div>
<% end %>
</div>
<% end %>
<% end %>