No method error when using pagy gem -Ruby on Rails - ruby-on-rails

I am trying to use pagy gem but I am getting no method error.
In my controller, I previously have
def index
#hires = [*current_user.student_hires.order('created_at desc')]
current_user.groups.includes(:group_hires).each do |group|
#hires.push(*group.group_hires.order('created_at desc'))
end
#hires = #hires.uniq(&:id)
end
but since I want to use Pagy, I changed it to
def index
#pagy, #hires = pagy([*current_user.student_hires.order('created_at desc')])
current_user.groups.includes(:group_hires).each do |group|
#hires.push(*group.group_hires.order('created_at desc'))
end
#hires = #hires.uniq(&:id)
end
and in my view, I have
<%== render partial: 'pagy/nav', locals: {pagy: #pagy} %>
But I am getting
undefined method `offset' for [#<Hire id: 12, grade: "Grade 1"
I am using pagy in another simpler controller and it works well but I can't get it to work on this controller index.

I was able to fix it using pagy_array
So I did
#pagy, #hires = pagy_array([*current_user.student_hires.order('created_at desc')])
and also added require 'pagy/extras/array' to the config/initializers
More instruction about it is here https://ddnexus.github.io/pagy/extras/array#gsc.tab=0

Related

Linking to the latest post from home page in a Rails application

I'm building a Rails app where I have individual entries called films. I would like to display the latest entry's link on the homepage (separate controller) and I'm struggling to make it work.
My films_controller.rb is as follows (excerpt):
def show
#film = Film.find(params[:id])
end
My home_controller.rb only has the following:
def index
end
And my view file (index.html.erb) has the following:
<%= link_to #film.last.filmTitle, film_path(#film) %>
I'm getting the following error:
Couldn't find Film with 'id'=#<Film::ActiveRecord_Relation:0x007fc93f2d1fd0>
With the #film.find(params[:id]) highlighted.
Thanks!
The last method:
Find the last record (or last N records if a parameter is supplied). If no order is defined it will order by primary key.
source
You can add a #last_film instance variable in your index controller and use it in the view.
def index
#films = Film.all
#last_film = Film.last
end
and in your index.html.erb
<%= link_to #last_film.filmTitle, film_path(#last_film) %>
The index method need something, currently, it didn't connect with ActiveRecord like model or table, that's why
Couldn't find Film with 'id'=#<Film::ActiveRecord_Relation:0x007fc93f2d1fd0>
So if you need to show recent posts in the index then you could something like this
def index
#films = Film.limit(10).order(created_at: :desc) #=> or you can use id
end
it will show last 10 records, for this in the index.html.erb like this
<% #films.each do |film| %>
<%= link_to film.filmTitle, film_path(film) %>
<% end %>
In the other hand if you need to show only one post which is the last then you should modify this query like this like limit(10) to limit(1) or you can use use the last method like this
def index
#film = Film.last
#or
##films = Film.limit(1).order(created_at: :desc) #=> or you can use id
end
if you use this #film = Film.last then your index file will like this
<%= link_to #film.filmTitle, film_path(#film) %>
otherwise, you need to use each method which describes before.

undefined method `group_by_day' error rails

I have installed gem groupdate and gem chartkick and already restart my server. I'm enable to display the graph that sorted by group_by_day at my show.html.erb. I'm using postgresql database
show.html.erb
<%= #material.current_stock %>
<%= line_chart #material.group_by_day(:updated_at).sum(:current_stock) %>
controller/material_controller.rb
def show
#material = Material.find(params[:id])
end
it's working fine when I declare using the class
<%= line_chart Material.group_by_day(:updated_at).sum(:current_stock) %>
but not when I replace Material with #material.
It should be work with Class method and array ,
controller/material_controller.rb
def show
#material = Material.where(id:params[:id])
end
Change with this your controller method
The group_by_day works for either a whole class or a group of objects, there is no use to group a single #material object.
For the whole class you can use,
Material.group_by_day(:updated_at).sum(:current_stock)
For group of materials,
#materials = Material.where('your logic')
#materials.group_by_day { |u| u.updated_at }.sum(:current_stock)
But if you want to group for a single object try,
#material = Material.where('your logic').first
#material.group_by_day { |u| u.updated_at }.sum(:current_stock)

How to get class name with orm_class on custom scaffold generator

I am making custom templates to help the development on our application and I got stuck in a silly issue.
I am trying to put the ransack parameter in the index action controller but I cant make the generator spell only the classname.
The default controler is
def index
#<%= plural_table_name %> = <%= orm_class.all(class_name) %>
end
Which will produce a thing like:
def index
#stuffs = Stuff.all
end
What I want is something like:
def index
#search = <%= orm_class.search(class_name, params[:q]) %>
#<%= plural_table_name %> = #search.result.order('id')
.paginate(page: params[:page], per_page: 50)
end
that would produce:
def index
#search = Stuff.search(params[:q])
#stuffs = #search.result.order('id')
.paginate(page: params[:page], per_page: 50)
end
The thing is, orm_class doesn't have a search method.
I could solve this if the function give me only "Classname" but if I try
orm_class alone, it return in the code "Rails::Generators::ActiveModel".
How can I make the orm_class give me only "Classname"?
The "best" I've come so far was
#search = <%= orm_class.all(class_name) %>.search(params[:q])
But then I have manually remove the "all" after I generate the scaffold.
ActiveSupport::Inflector adds a variety of methods for transforming strings. You want classify:
plural_table_name.classify
For example:
> "my_stuffs".classify
=> "MyStuff"
In your case:
#<%= plural_table_name %> = <%= plural_table_name.classify %>.search(params[:q])

Will_paginate in Dynamic pages as "search"

I'm having trouble with will_paginate. It works perfectly with static pages but not in dynamic pages. In my controller I have:
def search
#prods = Prods.find_all_by_producer(params[:producer])
#items = #prods.paginate(:page => params[:page], :per_page => 10)
end
In my view:
<%= will_paginate #items %>
The first 10 items (the first page) are well displayed but when I try to navigate to next pages, I have:
undefined method `paginate' for nil:NilClass
Parameters : {"page"=>"2","locale"=>nil}
I understand the issue, there is no params[:producer] when it calls the second page so #prods returns nil. But how to do that, any ideas?
Add the #prods as param like this:
{:producer=>#producer} %>
Source: http://www.cowboycoded.com/2009/09/08/appending-parameters-on-a-will_paginate-link/

NoMethodError when calling a helper method in a view

I'm working on a simple timesheet plugin for Redmine, all was going well until I tried to use helpers.
The helper:
module TimesheetHelper
def first_day_in_week(datum)
return unless datum.kind_of? Date
datum - datum.wday
end
def last_day_in_week(datum)
return unless datum.kind_of? Date
datum + (6 - datum.wday)
end
end
In the view I have
helper "timesheet"
But I've also tried
helper :timesheet
and
helper TimesheetHelper
In the first line of index.rhtml it says
<h2><%= l :timesheet_for %> <% first_day_of_week #week %> <%=l :and %>
<% last_day_of_week #week %></h2>
and rails throws a NoMethodError on first_day_of_week #week
Is there something I'm missing?
Your method is
def first_day_in_week(datum)
not
def first_day_of_week(datum)
The name is not the same, so the method is not found ^^

Resources