Trouble with pagination (will_paginate) - ruby-on-rails

I'm trying to paginate with will_paginate in my rails app.
in my controller I have
def index
params[:per_page] ||= 25
params[:page] ||= 1
#links = Link.order('created_at DESC').page(params[:page]).per_page(params[:per_page])
end
in my view I have
<ul>
<% #links.each do |entry| %>
<li><%= link_to entry.title, entry.url %></li>
<% end %>
<ul>
<%= will_paginate #links %>
I'm getting the error
comparison of Fixnum with String failed
Extracted source (around line #8):
5: </h2>
6:
7: <ul>
8: <% #links.each do |entry| %>
9: <li><%= link_to entry.title, entry.url %></li>
10: <% end %>
11: <ul>
I have no idea why. I have tried restarting the server. Am I missing something obvious?
Thanks in advance.

I'm guessing that you have a simple "everything in params is a String" problem, that could explain why someone is trying to compare a Fixnum with a String. The page and per-page values should be Fixnums but, if they come from params, they will be strings. The easiest thing to do is to add a couple to_i calls as indicated:
def index
params[:per_page] ||= 25
params[:page] ||= 1
#links = Link.order('created_at DESC').page(params[:page].to_i).per_page(params[:per_page].to_i)
#---------------------------------------------------------^^^^
#------------------------------------------------------------------------------------------^^^^
end
The query that paginate produces won't be evaluated until you try to get something from it, that's why you don't get the error until you #links.each in your template.

Related

Print pg_search params to Rails 5 template page

I have a products page that is filterable with pg_search gem. What I would like to do is create a breadcrumb that lists the search params onto the page.
If my search url looks like http://127.0.0.1:3000/products?utf8=%E2%9C%93&query=modern&query=stone&query=limestone&commit=Search I would like to print modern stone limestone to the page.
products_controller.rb
def index
#products = if params[:query]
#albums = Album.where(name: params[:query])
Product.search_for(params[:query])
else
#albums = Album.where(name: 'products')
Product.order(:name)
end
end
index.html.erb
This is what I tried, but realized that it lists ALL the tags, which I only need the tags that are part of the search params
<ul class="product-index-breadcrumb">
<% #products.each do |p| %>
<% p.tags.each do |t| %>
<li><%= t.name %></li>
<% end %>
<% end %>
</ul>
You need to modify your URL e.g array string adding [] if you keep this then it only without [] then it takes only last value like limestone,
after modification URL then it will look like this http://http://127.0.0.1:3000/products?utf8=%E2%9C%93&query[]=modern&query[]=stone&query[]=limestone
then if you write like
<%= params[:query] %>
it will return
["modern", "stone", "limestone"]
now you can run a loop like
<ul>
<% params[:query].each do |query_params| %>
<li><%= query_params %></li>
<% end %>
</ul>
then output is
modern
stone
limestone

Rails undefined method `id' for nil:NilClass

I'm earning rails with the guidebook, but some code does not run as expected (use Rails 4.2.6, but book was written about older version). Appreciate if you can help me.
When i load pages of any of my objects (Ads) - i see nice page with object parameters, but when I load page with list of objects - I get
NoMethodError in Ads#index
Showing /home/mei33/mebay/app/views/ads/index.html.erb where line #11 raised:
undefined method `id' for nil:NilClass
<ul>
<% for ad in #ads %>
<li><%= #ad.name %></li>
<% end %>
</ul>
my ads_controler.rb looks like that:
class AdsController < ApplicationController
def show
#ad = Ad.find(params[:id])
end
def index
#ads = Ad.all
end
end
tried to add this line of code, but not helped:
def new
#ad = Ad.new
end
maybe there is something I cannot notice? some mistake?
You should call as ad.id not #ad.id:
<ul>
<% for ad in #ads %>
<li><%= ad.name %></li>
<% end %>
</ul>
Or:
<ul>
<% #ads.each do |ad| %>
<li><%= ad.name %></li>
<% end %>
</ul>

Undefined method `each' for nil:NilClass with `order` and `limit` in controller

I'm trying to create a simple list of top 10 users ranked on the variable manpoints (a column in my users table), but keep getting:
undefined method `each' for nil:NilClass
Here's my erb:
<h1 class="text-center">Manliest Men</h1>
<ol>
<%= #manliest_men.each do |user| %>
<li><%= user.first_name %></li>
<% end %>
</ol>
And here's my controller for the index page where that erb lives:
def index
render layout: 'nofooter'
#manliest_men = User.order("manpoints DESC").limit(10)
end
I feel like this is fairly simple, but I can't figure out where I went wrong. Can anyone give me a hint?
Check this: Similar question
Quoting #Dylan Markow
You should be using <%, not <%= for your .each line, so
<%= #manliest_men.each do |user| %>
should be
<% #manliest_men.each do |user| %>
Edit:
The actual problem was in the controller, particularly the order:
def index
render layout: 'nofooter'
#manliest_men = User.order("manpoints DESC").limit(10)
end
Should be:
def index
#manliest_men = User.order("manpoints DESC").limit(10)
render layout: 'nofooter'
end

dealing with complex GET params in rails

I'm trying to pass params via GET to a form using ransack, it would normally work fine except that the params[:q] is causing a problem when I try to merge it.
controller (using ransack)
def index
#search = Record.ransack(params[:q])
#records = #search.result.page(params[:page])
end
view
<ul>
<% Genre.all.each do |genre| %>
<% category_count = #search.result.joins(:genre).where("genres.id = ?", genre.id).size %>
<% unless category_count == 0 %>
<li>
<%= link_to genre.name, params.merge(:"q[genre_name_eq]" => genre.name) %> (<%= category_count %>)
</li>
<% end %>
<% end %>
</ul>
But I keep getting duplication:
q[genre_name_eq]=Rockabilly&q[genre_name_eq]=Rockabilly
Without the q[] it manages the params correctly, overwriting the previous one.
Also tried params[:q].merge(:genre_name_eq) which causes the problem when there is no q[] and it also just doesn't work, giving me genre_name_eq= and no q[]
How else can I handle the q[] so that it replaces the value instead of duplicating it several times?
EDIT (after your edit)
params is a hash, not a simple querystring. If you want to update a nested value, you have to update the nested hash.
First ensure that params[:q] exists and is a hash (in the controller)
params[:q] ||= {}
Then only update q in the view
<% params[:q].update :genre_name_eq => genre.name %>
<%= link_to genre.name, params %>

get specific element from ruby block, rails app

When performing a block like:
<% #user.favoured_user.each do |user| %>
<li><%= user.name %></li>
<% end %>
With the favoured_user method returning a limit of 5 users, how would I manipulate the block so that even when there are only 3 users available, I could still return 5 li elements?
I'm guessing a helper would come in to play, and maybe the 'first, second, third, etc.' array methods, but I can't think how to write it.
Any help?
You can try this,
<% 5.times do |i| %>
<li> <%= #user.favoured_user[i].try(:name) %> </li>
<% end %>
You can use in_groups_of
Like:
<% #user.favoured_user.in_groups_of(5).each do |favored_user| %>
<% favored_user.each do |user| %>
<li><%= user.try(:name) %></li>
<% end %>
<% end %>
The first 3 users will come through, and the last two entries will be nil

Resources