Question

I've watched episode about Kaminari at railscasts.com and tried to add this system to my blog. But this error appears again and again and I don't understand why.

index action from posts controller:

  def index
@posts = Post.order("title").page(params[:page]).per(3)

if params[:tag]
  @posts = Post.tagged_with(params[:tag])
else
  @posts = Post.all
end

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @posts }
  end
end

view file: index.html.erb:

<%= paginate @posts%>

<% @posts.each do |post| %>

<h2><%= link_to post.title, post %></h2>
  <p>
    Created: <%= post.created_at.strftime("%Y/%m/%d")%> by
     <%=  link_to post.user.fullname, user_posts_path(post.user) %>
  </p>

  <p>
  <%= simple_format post.text %>
  </p>

  <p>
Tags: <%= raw post.tag_list.map { |t| link_to t, tag_path(t) }.join(', ')      %>
  </p>

  <% end %>
Was it helpful?

Solution

You're declaring @posts twice in your controller. Try something like this:

if params[:tag]
  @posts = Post.tagged_with(params[:tag]).order("title").page(params[:page]).per(3)
else
  @posts = Post.order("title").page(params[:page]).per(3)
end

After removing the top @posts = Post.order("title").page(params[:page]).per(3) line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top