undefined method `page' for #<ActiveRecord::Relation:0x000001054d1a38> - ruby-on-rails-3

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

Related

will_paginate -ActionView::Template::Error variable appears to be empty

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

undefined method total_pages in will_paginate gem

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

Pagination in ruby on rails

I had got a task to do pagination.When i am doing pagination i had got these error
NoMethodError in ProductsController#index
undefined method `page' for []:ActiveRecord::Relation
Rails.root: /home/nithinv/store
Application Trace | Framework Trace | Full Trace
app/controllers/products_controller.rb:4:in `index'
This is my controller
def index
#products = Product.order(:name).page(params[:page]).per(2)
respond_to do |format|
format.html #index.html.erb
format.json { render :json=> #products }
end
end
This is my index.html.erb
<% title "Products" %>
<%= paginate #products %>
<% for product in #products %>
<div class="product">
<h2><%= link_to product.name, product %></h2>
<div class="details">
<%= number_to_currency(product.price) %> |
Released <%= product.released_at.strftime("%B %e, %Y") %>
</div>
</div>
<% end %>
<p><%= link_to "New Product", new_product_path %></p>
How can i solve this problem?
Ref this
I think you forgot to add following line in your gemfile
gem 'kaminari'
and then bundle install
I looks that the problem has to do with kaminari gem configuration (not your code). I'd recommend you to do this quick check:
Run bundle install
Restart the server (because changes may not have been loaded)
Check your gemfile because, kaminari gem may is misplaced (under :test group)
A bit obvious, but in occasions I've also found myself wasting time after forgetting some basic checks.

NoMethodError / undefined method `foobar_path' when using form_for

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

Rails: Content_for in partial

I have something like this in my layout
...
<%= yield :test %>
...
<%= render :partial => 'user/bar' %>
And in user/bar.html.erb I have
<% content_for :test do %>
stuff
<% end %>
And this doesn't seem to work. And I have found out that yield :test executes before partial, but after executing the view of the action. Why does it do so and what can I do?
The syntax content_for :test do ... end captures the content of the block, and content_for :test gives the captured block. doc for content_for.
In your code, the restitution is done before the capture, so it cannot work.
I wrote the partial into a local variable before calling yield, and then rendered it into the document later:
...
<% partial = render(:partial => 'user/bar') %>
<%= yield :test %>
...
<%= partial %>