I have got an error in will_paginate gem
In the controller
#like_posts = current_user.likes.order("created_at DESC").page(params[:page]).per_page(10).map(&:post)
When I use in the view
<%= will_paginate(#like_posts) %><br/>
<%= page_entries_info(#like_posts) %><br/>
I got this error
undefined method `total_pages' for #<Array:0xaae2dec>
24: <%= will_paginate(#like_posts) %><br/>
25: <%= page_entries_info(#like_posts) %><br/>
and if i change make this in the controller
#like_posts = current_user.likes.order("created_at DESC").map(&:post).page(params[:page]).per_page(10)
i have this error
undefined method `page' for#<Array:0xabcd34c>
any help?
the main problem of me that i want the pagination to work on static array and it is very dangerous so will_paginate refuse it and like to work with the query from the database
here is the trouble shooting of will_paginate
https://github.com/mislav/will_paginate/wiki/Troubleshooting
i solve it by make 2 objects
#likes = current_user.likes.order("created_at DESC").page(params[:page]).per_page(5)
#like_posts = #likes.map(&:post)
in view
<%= will_paginate #likes %><br/>
<%= page_entries_info #likes %><br/>
use #likes for pagination and #like_posts to iterate on posts
That may be solved by adding require 'will_paginate/array' at the top of your application_controller.rb:
require 'will_paginate/array'
class ApplicationController < ActionController::Base
end
Related
I'm trying to implement a simple index page for one on my models that uses the will_paginate gem. (Rails 3) and am receiving the following error:
ActionView::Template::Error (The #load_verifications variable appears to be empty. Did you forget to pass the collection object for will_paginate?)
My code is the same (as far as I can tell) as all the other index pages I'm using will_paginate on that work.
LoadVerificationsController.rb:
def index
#loadVerifications = LoadVerification.paginate(page: params[:page], per_page: 10).order('ship_date')
end
index.html.erb:
<% provide(:title, 'All Loads')%>
<h1>All Loads</h1>
<%= will_paginate %>
<ul>
<%= render #loadVerifications %>
</ul>
<%= will_paginate %>
_load_verification.html.erb:
<li>
<%= loadVerification.sales_order %>
<%= loadVerification.ship_date %>
<%= loadVerification.pallet_count %>
<%= link_to "view", loadVerification %>
Any help would be appreciated. I'm not sure where the #load_verification variable referenced in the error message comes from since I haven't declared it in the controller and the variable I'm using is #loadVerification (no underscore). I will also mention that I don't currently have any data in the LoadVerifications table, but I would think that will_paginate is smart enough to handle an empty result set without throwing exceptions.
Thanks!
As official will_paginate gem documentation says: you should pass your collection (#loadVerifications in your case) as a parameter to will_paginate view helper:
<%= will_paginate #loadVerifications %>
Here's the source code which raises this error.
method call from within will_paginate method.
method declaration.
Briefly: if no collection is given to will_paginate helper, it tries to build instance variable's name from controller's name
trying to add pagination to my app (pages numbers at the bottom of the page)
getting error "undefined method `page' for #"
added 2 gems 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'
ran bundle install & rails s
added the line to my app/controllers/pins_controller.rb
def index
#pins = Pin.order("created_at desc").page(params[:page]).per_page(20)
end
Added pagination to the pins index view - app/views/pins/index.html.erb
<%= render 'pages/home' %>
<div id="pins">
<%= render #pins %>
</div>
<%= will_paginate #pins %>
but get this error message
NoMethodError in PinsController#index
undefined method `page' for #
app/controllers/pins_controller.rb:6:in `index'
fixed ! Change <% will_paginate #pins %> to <%= will_paginate #pins %>
I'm trying to make an invoicing app. The form to create an invoice should include a collection of check boxes so the user can choose which lessons to invoice, but I'm getting this error: undefined method 'collection_check_boxes'.
Here are the models involved:
class Lesson < ActiveRecord::Base
attr_accessible :lesson_time, :lesson_date, :invoice_id
belongs_to :invoice
end
class Invoice < ActiveRecord::Base
attr_accessible :amount, :due_date
has_many :lessons
end
And the view:
<%= form_for(#invoice) do |f| %>
<fieldset>
<%= f.label :lessons %>
<%= f.collection_check_boxes(:lessons, Lesson.all, :id, :lesson_date) %>
<%= f.submit %>
</fieldset>
<% end %>
collection_check_boxes is not a method of form_builder. Either put:
<%= collection_check_boxes(:lessons, Lesson.all, :id, :lesson_date) %>
This will generate html which won't associate with your model (you won't be able to use MyModel.new(params[my_model]) and expect to get proper response. You would either have to manually call my_model.lessons = params[:lessons] or you can pass a html name parameter to conform your check box name to rails convention).
Or, if you are using formtastic as you tagged it, you can use this:
<%= f.input :lessons, :as => :check_boxes, :collection => Lesson.all %>
I suspect that since you tagged your post ruby-on-rails-3, you might be trying to use a rails 4 method inside a rails 3 project.
http://makandracards.com/makandra/32147-rails-4-introduced-collection_check_boxes
You'll likely need to use good old check_box_tag instead.
I'm using form_for to create a chatroom and when I view the page I get the following error:
NoMethodError in Chatrooms#new
undefined method `chatrooms_path' for #<#<Class:0xa862b94>:0xa5307f0>
Here's the code for the view, located in app/views/chatrooms/new.html.erb:
<div class="center">
<%= form_for(#chatroom) do |f| %>
<%=f.text_field :topic%>
<br>
<%=f.submit "Start a discussion", class: "btn btn-large btn-primary"%>
<% end %>
</div>
Here's the relevant controller:
class ChatroomsController < ApplicationController
def new
#chatroom = Chatroom.new
end
def show
#chatroom = Chatroom.find(params[:id])
end
end
If I change the line
<%= form_for(#chatroom) do |f| %>
to
<%= form_for(:chatroom) do |f| %>
it works fine.
I've searched around for similar questions but none of the solutions have worked for me. Help?
It is because you didn't create route/action for ChatroomsController. When you render new form it is pointing to create action by default, if you want to change handler action, use
form_for #chatroom, :url => some_other_path
I am using Rails 3.0, Ruby 1.9.2 and the Plataformatec simple_form gem. This code works with a form_for but not simple_form_for:
<%= simple_form_for(#provider) do |f| %>
<% Car.all.each do |c| %>
<div>
<%= check_box_tag :car_ids, c.id, #store.cars.include?(c), :name => 'store[car_ids][]' %>
$<%= c.cost %> | <%= c.description %>
</div>
<% end %>
<div class="actions">
<%= f.submit "New" %>
</div>
<% end %>
How do I get it to work with simple_form_for?
Thanks in advance!
You can't use simple_form right the same way as form_for.
For example ther is no any check_box_tag method in simple_form gem. There is ONLY inuput fields that you can specify with :as option. So your check_box_tag will be converted to
f.input car_ids, ..., :as => :check_box
Checkout Usage, Rdoc and other useful stuff https://github.com/plataformatec/simple_form
The problem was in the controller code.
In the "new" controller action I can't simply perform:
#provider = Provider.new(params[:provider])
as one would normally.
Instead I have to process each parameter separately:
#provider.location = params[:provider][:location]
etc...
For the Car check boxes, I add each car_id from the car_ids parameter to the "has_many" cars model association one at a time:
car_ids = params[:provider][:car_ids]
car_ids.each do |cid|
#provider.cars << Car.find(cid)
end
Then I can call:
#provider.save!
And it saves correctly (my initial problem was that it wasn't saving the selected Cars).
For some reason, I was able to figure this out only after posting the question here. Funny how that works.
Thanks all for your replies!