I am using kaminari library, and I want to display users' pictures.
In my UserController, I put
def index
#users = User.order(:name).page params[:page]
end
Before, I was accessing the pictures like
#users.transitions-enabled
- #users.each do |user|
- user.pictures.each do |pic|
.box.panel.panel-default
= link_to (image_tag(pic.image.url(:origin))), user
I am now wondering how I can display images by using the following command:
<%= paginate #users %>
Edit:
Now I changed the code in UserController to
#users = User.all
#pictures = #users.pictures.page(params[:page]).per(2)
And it is giving me the error
undefined method `total_pages' for nil:NilClass
I was following the answer here but I am not sure how I can apply it in my case.
Related
I have a model Person and I want to list all 'people' on the home page of my website.
I created a partial _index.html.erb for this, so I could use this list everywhere on my website. On index.html.erb, I render this partial.
Now both http://localhost:3000/people/ and http://localhost:3000/people/index run fine. However, I also wanted to use the index partial on my home page. There, it gives the following error:
undefined method `each' for nil:NilClass
Some parts of my code:
people/_index.html.erb
<h2>The following people exist:</h2>
<table>
<tr>
<th>Name</th>
</tr>
<% #people.each do |p| %>
<tr>
<td><%= p.name %></td>
</tr>
<% end %>
</table>
people/index.html.erb
<%= render 'people/index' %>
pages/index.html.erb (This is my home page)
<h1>Homepage</h1>
<%= render 'people/new' %>
<%= render 'people/index' %>
routes.rb:
Rails.application.routes.draw do
root 'pages#index'
get 'pages/index'
get 'people/index'
resources :people
end
Part of people_controller.rb:
class PeopleController < ApplicationController
def index
#people = Person.all
end
def show
#person = Person.find(person_params)
end
...
Somehow, rendering people/new (also a partial) works perfectly fine if I comment out <%= render 'people/index' %>.
Searching for this error leads mainly to the solution "you didn't properly define #people in the controller". However, why does it then work fine on the /people/index page but not when rendering it on the homepage? Is it a routing issue?
I hope that my question is clear and that I posted enough code snippets, thanks in advance for your help!
Your PagesController, which is called when you hit the pages/index route when rendering the homepage, doesn't have #people defined, so #people ends up being nil, and the error occurs.
You need to add #people = People.all somewhere in your PagesController, since PeopleController doesn't actually get called when you access the homepage.
If you check your development.log you will see PeopleController does not get called when you access the homepage and the people/_index partial renders.
undefined method 'each' for nil:NilClass means that you are calling each on something you think is there, but isn't. In this case your calling #people.each, but #people is actually nil. So you see that error. In your controller set #people = People.all in the action/method that is actually fueling that page. So if you are on the show page, it would go in the show method. On the edit page, makes sure that code is in the edit method.
I'm new into coding and even more new with Rails.
I'm trying to create a simple blog application, I've the articles list on the index with their title, content and created at (I used time_ago_in_words for this) however, when I try to use it inside the show view of an article it returns me an error.
With <%= #article.created_at(time_ago_in_words) %> it returns
wrong number of arguments (0 for 1..2) on the line
<span class="meta">Posted by Author <%= #article.created_at(time_ago_in_words) %></span>
With <%= #article.time_ago_in_words(article.created_at) %> it returns
undefined local variable or method `article' for #<#:0xb4ba430c> on the line
<span class="meta">Posted by Author <%= #article.time_ago_in_words(article.created_at) %></span>
My articles controller
class ArticlesController < ApplicationController
def index
#articles = Article.all.order("created_at DESC")
end
def show
#article = Article.find(params[:id])
end
def new
#article = Article.new
end
end
So I would like to know what is the properly way to display the created_at date but in words, because if I use <%= #article.created_at %> it works fine but the date it returns something like 2015-08-17 15:49:11 UTC which doesn't seems good.
Any help is highly appreciated.
The correct syntax is <%= time_ago_in_words(#article.created_at) %>
I saw some similar questions but didn't success with this.
I want to display the 3 latest posts on my homepage.
I define this method on my PostsController
def noticia
#posts = Posts.all(:limit => 3)
end
helper_method :noticia
and I invoke this on my view
- if #noticia
%h4.feed
A Sair
%h6
%span= #noticia.created_at.strftime("%d %b. %Y")
= link_to #noticia.content, posts_path
%p
- if current_admin
= link_to "Adicionar notÃcia", new_post_path
It gives NoMethodError
undefined method `each' for #<Post:0x00000102fb6fc8>
There is a lot of strange things in your code.
Your noticia method should be :
def noticia
#posts = Post.order("created_at desc").limit(3)
end
You don't need to use helper_method.
And your view file must be something like :
- if #posts.any?
- #posts.each do |post|
= # do something with my post
Hope it helps!
#posts = Post.order('created_at').limit(3)
#posts = Post.order('created_at DESC').limit(3)
#posts = Post.order('created_at ASC').limit(3)
Is your model 'Post' (not Posts)?
This is how you'd use limit with ActiveRecord.
def noticia
#posts = Post.limit(3)
end
I searched and searched, but nothing solved my problem. Here's my controller:
def show
#topic = Topic.find(params[:id])
#topic.posts = #topic.posts.page(params[:page]).per(2) # 2 for debugging
end
That functions just fine, because the topic view is reduced to two posts. However, when I add this to show.html.erb:
<%= paginate #topic.posts %>
I'm given this error:
undefined method `current_page' for #<ActiveRecord::Relation:0x69041c9b2d58>
Try with:
def show
#topic = Topic.find(params[:id])
#posts = #topic.posts.page(params[:page]).per(2)
end
And then:
<%= paginate #posts %>
If you get pagination errors in Kaminari like
undefined method `total_pages'
or
undefined method `current_page'
it is likely because the AR scope you've passed into paginate has not had the page method called on it.
Make sure you always call page on the scopes you will be passing in to paginate!
This also holds true if you have an Array that you have decorated using Kaminari.paginate_array
Bad:
<% scope = Article.all # You forgot to call page :( %>
<%= paginate(scope) # Undefined methods... %>
Good:
<% scope = Article.all.page(params[:page]) %>
<%= paginate(scope) %>
Or with a non-AR array of your own...
Bad:
<% data = Kaminari.paginate_array(my_array) # You forgot to call page :( %>
<%= paginate(data) # Undefined methods... %>
Again, this is good:
<% data = Kaminari.paginate_array(my_array).page(params[:page]) %>
<%= paginate(data) %>
Some time ago, I had a little problem with kaminari that I solved by using different variable names for each action.
Let's say in the index action you call something like:
def index
#topic = Topic.all.page(params[:page])
end
The index view works fine with <%= paginate #topic %> however if you want to use the same variable name in any other action, it throu an error like that.
def list
# don't use #topic again. choose any other variable name here
#topic_list = Topic.where(...).page(params[:page])
end
This worked for me.
Please, give a shot.
Apologies in advance, I am a newbie trying to get my head around rails.
My View at the bottom works when I use:
def show
#posts = Post.all
end
However in my controller I now have:
def show
#posts = Post.find_by_category_id params[:id];
end
In my view I have
<%= #posts.each do |post| %>
<%= post.title %>
<% end %>
Some please explain why I get this error. What should I use. category_id is a foreign key on the Post table.
Look at http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
Finds the first record matching the specified conditions
find_by_ will return only one post, not a collection. So you are not able to use each.
try
def show
#posts = Post.all.find_by_category_id params[:id];
end