undefined method username after update
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)
end
def show
@forum_post = ForumPost.new
@forum_posts = @forum_thread.forum_posts.paginate(:page => params[:page], :per_page => 2)
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
end
def update
if @forum_thread.update(forum_thread_params)
redirect_to @forum_thread
else
render 'edit'
end
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])
end
end
Error Log
ActionView::Template::Error (undefined method `username' for nil:NilClass):
1:
2: <%= div_for forum_post do %>
3:
Posted by <%= forum_post.user.username %> <%= time_ago_in_words forum_post.created_at %>
4:
<%= forum_post.body %>
5: <% end %>
If I remove the forum_post.user.username from the html it'll load just fine, I've tried adding @forum_post to the update but that didn't work either...