Rails3 - will_paginate plugin strange output - ruby-on-rails

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">&laquo; Previous</span> <span class="current">1</span> 2 Next &raquo;
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

Related

Name Error with will_paginate

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

error: undefined method `presigned_post'

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

How to implement in Rails "Showing 11 of 20 of 100 entries" paginate

I have this table in rails which has a lot of data. I have already implemented using kaminari paginate
<div class="row">
<div class="pull-right right-paginate"><%= paginate #applications %></div>
</div>
but i want to be able to do something like this "showing x to y of z entries" whereby in page 1 it says 'Showing 1 to 10 of 80 entries' and in page 2 it says "Showing 11 to 20 of 80 entries"
How is the best way to implement this is in rails using kaminari or any other gems.
Thanks
Kaminari has a helper method page_entries_info that will give you what you need. You can tweak the message by passing in options, or change it completely by modifying the corresponding entry in your locale file. The default locale configuration can be seen here - you may override them in your own locale config as you see fit.
Simple, use gem
gem "will_paginate", '~> 3.0'
In the controller for example
def index
#documents = Document.all.paginate(:page => params[:page], :per_page => 10, :order => 'created_at DESC')
respond_to do |format|
format.html # index.html.erb
format.json { render json: #documents }
end
end
And at the end in the index.html.erb add this:
<%= will_paginate(#movies, :renderer => 'BootstrapPaginationHelper::LinkRenderer') %>

Rails creating malformed routes with dots

I'm using the path helper methods to generate URLs in link_to, and they are returning URLs formated like this :
http://localhost:3000/tweets.4
when I was expecting them to be formated like this:
http://localhost:3000/tweets/4
Note how it is using a dot as the delimiter instead of the expected forward slash. The top link doesn't resolve to the correct view, it simply reloads the /tweets view. When I manually edit the URL to be like the bottom, it opens the correct /tweets/show/.
The closest thing I found in my online research was that people encountered this with wrongly nested routing statements - but I don't think I'm doing that here.
I would appreciate any help or pointers anyone can provide!
Here are the related source files and version information :
tweets/index.html.erb
<h1>Listing tweets</h1>
<% #tweets.each do |tweet| %>
<div>
<!-- creates path in format of /tweets.2 -->
<div><%= link_to tweet.status, tweets_path(tweet) %></div>
<!-- creates path in the format of /user.1 -->
<div><%= link_to tweet.user.name, users_path(tweet.user) %></div>
</div>
<% end %>
tweets_controller.rb
class TweetsController < ApplicationController
def index
#tweets = Tweet.all
end
def show
#tweet = Tweet.find(params[:id])
end
def new
#tweet = Tweet.new
end
def create
#tweet = Tweet.new(params[:tweet])
#tweet.user = User.last
if(#tweet.save)
redirect_to :root
end
end
def edit
#tweet = Tweet.find(params[:id])
end
def delete
end
end
routes.rb
Zombietweets::Application.routes.draw do
resources :tweets
root :to => 'tweets#index'
end
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.9'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
end
group :assets do
gem 'sass-rails', '3.2.3'
gem 'coffee-rails', '3.2.1'
gem 'uglifier', '1.0.3'
end
gem 'jquery-rails', '2.0.2'
I'm using Rails 3.2.9 and Ruby 1.9.3p327 (2012-11-10) [x86_64-darwin12.2.0]
Have you tried tweet_path and user_path ?
You want to access the show action. For that action, the model name must be singular in the *_path call.
To be sure, try a rake routes in a console.
EDIT:
You also forget to add resources :users in your routes file :)

will_paginate NoMethodError

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)

Resources