Ask A Question

Notifications

You’re not receiving notifications from this thread.

Number of posts on index / Posts pagination

Erick Sitter asked in Rails

I followed your forum series and it's going to work out well for me, but I couldn't figure out how to display the number of posts on the main index for each forum thread.

I'd think it would have been as simple as just parsing forum_thread.forum_posts in html.
It works, but just get a bunch of activerecord collection proxy errors.

I also wanted to paginate the posts and limit to 10 posts per page, now I can get pagination showing but it appears to be collecting all of the posts not from the specific forum thread.

All the code.

Forum_threads Controller:
class ForumThreadsController < ApplicationController
require 'forum_controller'
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread, except: [:index, :new, :create]

def index
@q = ForumThread.search(params[:q])
@forum_threads = @q.result(distinct: true)
@forum_threads = ForumThread.paginate(:page => params[:page], :per_page => 3)
end

def show
@forum_post = ForumPost.new
@forum_posts = ForumThread.find(params[:id])
@forum_posts = ForumPost.paginate(:page => params[:page], :per_page => 3)
end

def new
@forum_thread = ForumThread.new
@forum_thread.forum_posts.new
end

def create
@forum_thread = current_user.forum_threads.new forum_thread_params
@forum_thread.forum_posts.first.user_id = current_user.id

if @forum_thread.save
  redirect_to @forum_thread
else
  render action: :new
end

end

private

def set_forum_thread
  @forum_thread = ForumThread.find(params[:id])
end

def forum_thread_params
  params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:body])
end

end

Forum_Posts_Controller:
class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!
before_action :set_forum_thread
def create
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user

if @forum_post.save
  redirect_to forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id}"), notice: "Successfully posted!"
else
  redirect_to @forum_thread, alert: "Unable to save your post"
end

end

private

def set_forum_thread
  @forum_thread = ForumThread.find(params[:forum_thread_id])
end

def forum_post_params
  params.require(:forum_post).permit(:body)
end

end

Routes:
Rails.application.routes.draw do
devise_for :users, :path => '', :path_names => {:sign_up => 'register', :sign_in => 'login', :sign_out => 'logout'}
scope "/forum" do
resources :forum_threads do
resources :forum_posts, module: :forum_threads
end
end
end

Also I would like to reroute as of right now it's /forum/forum_threads/id, which is fine when you're viewing the post but to view the thread index it's /forum/forum_index/ I'd prefer to re-route that index without creating a controller to just /forum/ then to the posts can still be /forum/forum_threads/id.

Reply

It looks like in a couple of your controller actions, you're overwriting the variables incorrectly which might be the issue:

# show
@forum_posts = ForumThread.find(params[:id])
@forum_posts = ForumPost.paginate(:page => params[:page], :per_page => 3)

That first part there should probably be @forum_thread = ForumThread.find(params[:id]) so that you assign it the right name.

Then the second one, you need to reference that @forum_thread in order to use it's associated posts only. It should look like this: @forum_posts = @forum_thread.paginate(:page => params[:page], :per_page => 3)

I think if you get that fixed and any others like that, that should fix your issues with ActiveRecord.

Reply

Got it correctly working by @forum_posts = @forum_thread.forum_posts.paginate
But all the replies/posts are still showing and not limiting to 3 per page...

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.