Ask A Question

Notifications

You’re not receiving notifications from this thread.

Checking if Forum_Thread is updating?

Erick Sitter asked in General

Now the actual update is simple it's just @forum_thread.update params and redirect, but as you can see it redirects to the forum_thread which renders the show.html.erb which then causes @forum_post to create a new post which is causing duplicate entries after updating...

I thought a simple check with @forum_thread.update //nothing here else // new forum post end would solve it, no... just causes argument errors.

Also the user data isn't being supplied when updating, I have added @forum_post.user = current_user in the update but does nothing...

Forum Thread Controller

Forum Threads Controller

class ForumThreadsController < ApplicationController
  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)
  end

  def show
   if @forum_thread_updating = true
     # Nothing happens when updating
   else
     @forum_post = ForumPost.new
   end
  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

  def edit
    @forum_thread_updating = true
    @forum_post.user = current_user
  end

  def update
     if @forum_thread.update forum_thread_params and @forum_post.user = current_user
        redirect_to @forum_thread
     else
        render 'edit'
     end
  end

  def destroy
    @forum_thread.destroy
    redirect_to root_path
  end

  private

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

    def forum_thread_params
      params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:body], forum_posts_attributes.where([:id =>]))
    end
end
Reply

So one thing here is your set_forum_thread method is trying to do something that doesn't quite make sense. Your URL has an ID in it and that represents one single record, but you're using it to find two records. Really you want this to

Assuming you're using nested routes, you will have a URL like this: /forum_threads/:forum_thread_id/forum_posts/:id and you want to make sure you use the correct one of those params for looking up the different tables.

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

The forum_threads model doesn't have :forum_thread_id but forum_posts does.

I have the find :foumd_thread_id in the posts model.

Reply

Well, the params in the routes are used to lookup the ID column in the tables. You're using the same params id to look up two different records which doesn't quite make sense there. I would not set the forum_post in that before_action so that you don't run into weirdness.

Reply
Join the discussion
Create an account Log in

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

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

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