Ive been stuck for a while now in the rails tutorial. Im in section 10.3, where we are supposed to add pagination. I have added the gem 'will-paginate' to my Gemfile, and this is the index view
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<% #users.each do |user| %>
<li>
<%= gravatar_for user, :size => 30 %>
<%= link_to user.name, user %>
</li>
<% end %>
</ul>
<%= will_paginate %>
The server raises the following problem:
ActionView::Template::Error (undefined method `total_pages' for #<Array:0xa0283fc>):
1: <h1>All users</h1>
2: <%= will_paginate %>
3: <ul class="users">
4: <% #users.each do |user| %>
5: <li>
I have been trying to search for it.. but none of the solutions to apparently similar issues worked. Any ideas?
Just scroll lower in the tutorial - the next section addresses that problem :)
The view in Listing 10.27 doesn’t work yet, though, because currently #users contains the results of User.all (Listing 10.20), which is of class Array, whereas will_paginate expects an object of class WillPaginate::Collection.
It then has you modify the UsersController to fix it-
class UsersController < ApplicationController
before_filter :authenticate, :only => [:index, :edit, :update]
.
.
.
def index
#title = "All users"
#users = User.paginate(:page => params[:page])
end
.
.
.
end
I guess you are missing an argument to will_paginate:
will_paginate #users
Related
I'm having trouble with using link_to in Ruby on Rails. I'm making a blog like application which displays a feed of all user posts and allows users to click posts to view/edit them.
This is my _feed partial, which is used to render all the posts of a user.
<% if #feed_items.any? %>
<% #feed_items.in_groups_of(3, false).each do |feeds| %>
<div class="row">
<% feeds.each do |feed| %>
<div class="col-md-4">
<ol class="posts">
<%= link_to render feed , edit_post_path(feed) %>
</ol>
</div>
<% end %>
</div>
<% end %>
<% end %>
The line <%= link_to render feed , edit_post_path(feed) %> is what is throwing errors. I'm just not 100% certain how to write a link_to which also renders the feed. I've tried a lot of variations and nothing works. The error I get as it is currently written is: undefined method `keys' for "/posts/160/edit":String
This is my Posts controller, which I wrote after this error occurred in an attempt to fix it. I'm not sure if any of this is even necessary:
class PostsController < ApplicationController
before_action :find_note, only: [:show, :edit]
def edit
end
def show
end
private
def find_note
#post = Post.find(params[:id])
end
I'm sure my problem is super basic but I'm having trouble figuring out how to solve it. Any help is appreciated!
Use block to render a partial inside of a link:
<%= link_to edit_post_path(feed) do %>
<%= render feed %>
<% end %>
I am trying to make an app with Rails 4.
I have installed the public_activity gem.
I followed the Ryan Bates Railscast and took the controller based approach, and also the lighter Common option (as opposed to tracking the Model).
In my activities_controller I have:
class ActivitiesController < ApplicationController
def index
#activities = PublicActivity::Activity.order("created_at desc")
end
end
In my project.rb, I have:
include PublicActivity::Common
In my projects controller, create action, I have:
#project.create_activity :create, owner: current_user
In my activity view - index, I have:
<% Activities.each do |activity| %>
<div class="col-md-4">
<div class="indexdisplay">
<span class="indexheading">
<%= link_to activity.owner.name, activity.owner if activity.owner %>
</span>
<span class="indexsubtext">
<%= render_activity activity %>
</span>
In my public activity (view folder)/project/_create.html.erb, I have:
<% if activity.trackable %>
<%= link_to activity.trackable.name, activity.trackable %>
<% else %>
which has since been removed
<% end %>
When I try this, I get this error:
NameError at /activities
uninitialized constant ActionView::CompiledTemplates::Activities
I tried replacing the opening line of the activity#index so that Activities, is Activity, but it just changed the error message to:
NameError at /activities
uninitialized constant ActionView::CompiledTemplates::Activities
What does this error mean? How do I fix it?
Thank you
It seems like you use Class in your loop. Try to use your instance variable in your controller.
Change this
<% Activities.each do |activity| %>
into
<% #activities.each do |activity| %>
It should be <% Activity.find_each do |activity| %>
Model name always is singular
You cannot call each just on Model
I'd recommend you use find_each instead of each in case you have a lot of records
if you do want all the records you can always use .all method
http://apidock.com/rails/ActiveRecord/Batches/ClassMethods/find_each
I am using will_paginate with rails 4 and have the following code in my users controller:
def index
#users = User.paginate(page: params[:page], :per_page => 10)
end
and in my view:
<% provide(:title, 'All users') %>
<h1>All Users</h1>
<%= will_paginate %>
<ul class="users">
<% #users.each do |user| %>
<%= render #users %>
<% end %>
</ul>
<%= will_paginate %>
This renders all the users however there are 40 per page, not 10. Each page has the correct users but 4 times. For example it renders users 1 .. 10 then 1 .. 10 again and so on. There are currently 101 users and if I set the per page limit to 1 it renders 101 pages with one user on each page as it should however any limit > 1 and it breaks.
Any insight on how to fix it so only 10 users appear on each page would be much appreciated.
I found your mistake. You need to render #users once. Hope it works on your side. :)
<ul class="users">
<%= render #users %>
</ul>
also you need to render pagination with following code
<%= will_paginate #users %>
You need to reference the block variable user within the render call, not #users
<ul class="users">
<% #users.each do |user| %>
<%= render user %>
<% end %>
</ul>
I'm trying to put some code in a partial from my show page in /events. The code works fine when I use it in my show page, but when i extract it to a partial I'm getting a No Method Error
undefined method `event' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_PaperTrail_Version:0x007f4368823a68>
versions/_version.html.erb
<% cache version do %>
<div class="feed-item">
<h4>
<%= link_to version.item.name, version.item %>
<small>
<%= version.event + "d" %> by <%= link_to User.find(version.whodunnit).username, User.find(version.whodunnit) %>
</small>
</h4>
<ul class="list-unstyled">
<% version.changeset.each do |data| %>
<li>
<strong><%= data[0].capitalize %>:</strong>
<% if data[1][0].present? %><p class="red">- <%= data[1][0] %></p><% end %>
<p class="green">+ <%= data[1][1] %></p>
</li>
<% end %>
</ul>
</div>
<% end %>
show.html.erb
<%= render :partial => '/versions/version', :object => #versions %>
events controller
def show
#versions = #event.versions
end
events model
has_paper_trail
Any idea how i could put my code in a partial instead of having it inside my show view? Thanks.
EDIT:
I still get a No Method Error
undefined method `item' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_PaperTrail_Version:0x007f43688ee1a0>
events_controller.rb
before_action :set_event, only: [:show, :update, :destroy]
def set_event
#event = Event.find(params[:id])
end
Do you have defined the #eventvariable in the eventscontroller?
The #event.versions method has to be called on an actual event object.
For example
def show
#event = Event.find(1)
#versions = #event.versions
end
I have the following action method:
class CommandsController < ApplicationController
.
.
.
def usersList
#command = Command.find_by(id: params[:id])
#users_list = #command.user_commands.paginate(page: params[:page], per_page: 10)
end
end
The following view: users_list.html.erb
<% provide(:title, "Command list") %>
<h1>Lista Utenti per <%= #command.content %></h1>
<%= will_paginate #users_list %>
<ul class="users">
<% #users_list.each do |user| %>
<li><%= user.name.capitalize %>, <%= user.email %></li>
<% end %>
</ul>
And in the routes.rb
get 'commands/:id/usersList' => 'commands#users_list', as: :list
Now I don't understand why going to lochalhost:3000/commands/1/usersList I receive the following error:
undefined method `content' for nil:NilClass
refered to line <h1>Lista Utenti per <%= #command.content %></h1>
Why does it do so? When I call an action, before it renders the page and then after it executes the code inside the method?
I resolved the problem by changing the names: usersList --> userslist and then users_list.html.erb --->userslist.html.erb and in the routes.rb ----> get 'commands/:id/userslist' => 'commands#userslist', as: :list
Then I don't understand the naming convention...if I declare an action usersList how should I rename the related view?
You should change your action name to users_list.