Writing sample_app for railstutorial and having next error.
I've added <%= will_paginate #microposts %> to the user profile View and #microposts = #user.microposts.paginate(:page => params[:page]) to UsersController
> NoMethodError (protected method `wp_parse_options' called for #<Class:0x007fde66015a80>):
app/controllers/users_controller.rb:17:in `show'
Where is the problem?
Try gem 'will_paginate', '~> 3.0' and bundle install again, restart server. Give paginate some extra params just for the hell of it paginate(page: params[:page], per_page: 15)
Related
I'm trying to use will_paginate and have done everything listed in the github installation/use instructions, but I'm getting a NameError when I try to load the page:
uninitialized constant BooksController::Books
In my Gemfile I have:
gem 'will_paginate', '~> 3.1.0'
In my controller I have:
def index
#paginate books, have 10 per page
#books = Books.paginate :page => params[:page], :per_page => 10
end
And in my index.html.erb, I have added:
<%= will_paginate #books %>
Has anyone encountered this? I've run bundle install, and the gem appeared to be installed.
Use like this #books = Book.paginate :page => params[:page], :per_page => 10
Model name should not be plurals
I'm writing a rails application with which a user can upload images. I am deploying with Heroku, and using Carrierwave and S3 to upload and store images. I have followed this heroku guide step-by-step...unfortunately I am still getting an error "undefined method `presigned_post'", and do not know how to resolve it. It seems the S3_BUCKET is not being recognized as an aws object...
Has anyone come across this problem and figured it out? Here's some code for reference:
Pictures controller:
class PicturesController < ApplicationController
before_action :set_s3_direct_post, only: [:new, :create]
def index
#pictures = Picture.all
end
def new
#pictures = Picture.all
#picture = Picture.new
end
def create
#picture = Picture.new(picture_params)
if #picture.save
redirect_to new_picture_path, notice: "You just uploaded a picture!"
else
render "new"
end
end
...
def picture_params
params.require(:picture).permit(:attachment)
end
private
def set_s3_direct_post
#s3_direct_post = S3_BUCKET.presigned_post(key: "uploads/#{SecureRandom.uuid}/${filename}", success_action_status: '201', acl: 'public-read')
end
end
New picture view:
<h1>Upload a new picture</h1>
<br>
<div class="well">
<%= form_for #picture, html: { class: 'directUpload', data: { 'form-data' => (#s3_direct_post.fields), 'url' => #s3_direct_post.url, 'host' => URI.parse(#s3_direct_post.url).host } } do |f| %>
<%= f.file_field :attachment %>
<%= f.submit "Upload", class: "btn btn-default" %>
<% end %>
</div>
And config/environment.rb:
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
# S3
S3_BUCKET='fotoes'
AWS_ACCESS_KEY_ID='secretxxxxxxxx'
AWS_SECRET_ACCESS_KEY='xxxxxxxsecretxxxxxx'
Any thoughts?
#Jillian I suppose your expectation was that S3_BUCKET class should call presigned_post method which is supposed to be defined. However, it seems its not. I took a look at the heroku page containing the tutorial you followed and well you followed every instruction. I suggest you contact heroku about the documentation. However, I would keep looking into it
Thank you all for your help. In the end I found a different walk-through that was much simpler and effective. (The Heroku one was a little complicated and left a lot to go wrong - go figure.)
This solved everything :)
edit:
not everything - had to complete one last step before I got the site running. Run this line in the terminal: $ heroku config:add AWS_ACCESS_KEY=value
and $ heroku config:add AWS_SECRET_KEY=value, where the value is your S3 credentials for each, respectively.
This was the only combo of fog, carrierwave, rails, fog-aws gems that worked (after weeks of fussing with them):
gem 'rails', '4.1.0'
gem 'carrierwave', '~> 0.10.0'
gem 'fog', '1.34.0'
gem 'fog-aws', '0.7.6'
i resolved this by updating my version of aws-sdk
$ bundle update aws-sdk
def index
#users = User.order("name").page(params[:page]).per_page(5)
end
This is my UsersController where i try to view all users from database. Problem is because is see only this error:
undefined method `page' for #<ActiveRecord::Relation::ActiveRecord_Relation_User:0x000000044853d8>
Where is the problem?
You can do like this incase you have used will_paginate gem
#users = User.order("name ASC").paginate(:page => params[:page], :per_page => 5)
Take a look on the gem will_paginate
The usage of will_paginate is slight different than Kaminari. You should do:
#users = User.order('name').paginate(page: params[:page], per_page: 5)
So I am running a rails 2.3.11 app, with bundler and I am having the darndest time getting will_paginate to work. Its returning the following error:
undefined method `paginate' for #<Class:...
def show
#category = Category.find_by_url_name(params[:id])
#brands = #category.brands.find(:all, :order => "name")
#categories = Category.find(:all)
#meta_title = "#{#category.name}"
respond_to do |format|
format.html do |wants|
#brand = #brands.first
#products = Product.paginate(:page => params[:page], :per_page => 6, :conditions => ['brand_id = ? AND category_id = ?', #brand.id, #category.id])
render :template => 'brands/show'
end
format.xml { render :xml => #category }
end
end
this is my controller action. the full error:
NoMethodError in CategoriesController#show
undefined method `paginate' for #<Class:0x1034703e8>
RAILS_ROOT: /Users/tjs/Sites/emeraldcg
Application Trace | Framework Trace | Full Trace
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-2.3.11/lib/active_record/base.rb:1998:in `method_missing'
/Users/tjs/Sites/emeraldcg/app/controllers/categories_controller.rb:28:in `show'
/Users/tjs/Sites/emeraldcg/app/controllers/categories_controller.rb:25:in `show'
Request
Parameters:
{"id"=>"acoustic-guitars"}
Show session dump
Response
Headers:
{"Content-Type"=>"text/html",
"Cache-Control"=>"no-cache"}
my gemfile is pretty straight forward:
# A sample Gemfile
source "http://rubygems.org"
# gem "rails"
gem "rails", '2.3.11'
gem "rake"
gem "RedCloth", ">=4.2.2"
gem "will_paginate", "~> 2.3.16"
group :development do
gem "mysql"
gem "capistrano"
gem "capistrano-ext"
end
group :test do
gem "sqlite3"
end
I would understand if this failed due to it being an array but why is it failing as a class method?
thanks for the help.
In the past there where some problems with will_paginate and Rails 2, try to update to the latest version op will_paginate. In the meanwhile you can try this :
#products = Product.all.paginate(:page => params[:page], :per_page => 6, :conditions => ['brand_id = ? AND category_id = ?', #brand.id, #category.id])
You cannot call paginate on a model. It can be called on collections like hashes, array, ActiveRecord.
I have the will_paginate plugin working in an application, but when I paginate a resource it just spits out the HTML as text, doesn't provide links to the next pages and such.
And when I manually type in the URL the plugin is working it just doesn't make <%= will_paginate #products %> into links such as next 1 2 3 ... last
This is the output
<span class="disabled prev_page">« Previous</span> <span class="current">1</span> 2 Next »
controller:
def index
#products = Product.all.paginate :per_page => 5, :page => params[:page]
#product_categories = ProductCategory.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #products }
end
end
view
<%= will_paginate #products %>
<%= will_paginate %> #for some reasons this works too
source 'http://rubygems.org'
gem 'rails', '3.0.0.beta2'
gem "will_paginate", '3.0.pre'
if you run into troubles related to haml we use that version:
gem 'haml', '3.0.2'
will_paginate is now at this location:
gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git', :branch => "rails3"
update your gemfile
I believe the reasons is the ways rails3 escapes html and for whatever reason will_pagiante is getting escaped.
to fix this you first need to get the correct gem as the plugin won't work so add gem 'agnostic-will_paginate', :require => 'will_paginate' and that is done in the new gem file located in the app folder of a rails3 project.
After that you need to stop rails from escaping will_paginate with raw so something like
<%=raw will_paginate #products %> which is the opposition of <%=h will_paginate #products %> which in rails3 is equivalent to <%= will_paginate #products %>
WILL PAGINATE MOVED TO GITHUB. This repository is no longer updated.
It's recommended that you install the gem instead of a Rails plugin:
gem install will_paginate
and Try Again