"Undefined method paginate" 使用 will_paginate Gem 时出现错误消息
"Undefined method paginate" error message is coming while using will_paginate Gem
我在我的应用程序中实现 will_paginate
gem 时收到以下错误。
错误:
NoMethodError in ProductsController#index
undefined method `paginate' for #<Array:0x2d25ae8>
app/controllers/products_controller.rb:3:in `index'
请检查我下面的代码,请尝试帮助我解决这个问题。
宝石文件
source 'https://rubygems.org'
gem 'rails', '3.2.19'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'debugger'
gem 'will_paginate', '~> 3.0'
views/products/index.html.erb
<%= will_paginate @products %>
<table style="width:500px">
<thead>
<tr>
<th>Name</th>
<th style="text-align:right;">Price</th>
<th style="text-align:right;">Quantity</th>
</tr>
</thead>
<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td style="text-align:right;"><%= number_to_currency product.price %></td>
<td style="text-align:right;"><%= product.quantity %></td>
</tr>
<% end %>
</tbody>
</table>
<%= will_paginate @products %>
controller/products_controller
class ProductsController < ApplicationController
def index
@products = Product.all.paginate(page: params[:page], per_page: 5)
end
end
请帮助我。
啊,刚刚注意到你在 rails 3. 在那种情况下,试试这个:
@products = Product.paginate(page: params[:page], per_page: 5)
不同之处在于缺少 #all
,returns Array
rails 3.
我在我的应用程序中实现 will_paginate
gem 时收到以下错误。
错误:
NoMethodError in ProductsController#index
undefined method `paginate' for #<Array:0x2d25ae8>
app/controllers/products_controller.rb:3:in `index'
请检查我下面的代码,请尝试帮助我解决这个问题。
宝石文件
source 'https://rubygems.org'
gem 'rails', '3.2.19'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'debugger'
gem 'will_paginate', '~> 3.0'
views/products/index.html.erb
<%= will_paginate @products %>
<table style="width:500px">
<thead>
<tr>
<th>Name</th>
<th style="text-align:right;">Price</th>
<th style="text-align:right;">Quantity</th>
</tr>
</thead>
<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td style="text-align:right;"><%= number_to_currency product.price %></td>
<td style="text-align:right;"><%= product.quantity %></td>
</tr>
<% end %>
</tbody>
</table>
<%= will_paginate @products %>
controller/products_controller
class ProductsController < ApplicationController
def index
@products = Product.all.paginate(page: params[:page], per_page: 5)
end
end
请帮助我。
啊,刚刚注意到你在 rails 3. 在那种情况下,试试这个:
@products = Product.paginate(page: params[:page], per_page: 5)
不同之处在于缺少 #all
,returns Array
rails 3.