All threads / Devise: current_user not saving to @forum_post.user

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Devise: current_user not saving to @forum_post.user

Erick Sitter asked in Gems / Libraries

I'm currently having an issue when I create posts, I always seem to get undefined method "username" or "email", but when using current_user.username or current_user.email it will display just fine...

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)
@forum_threads = ForumThread.paginate(:page => params[:page], :per_page => 3)
end

def show
@forum_posts = ForumThread.find(params[:id])
@forum_post.user = current_user
@forum_post = ForumPost.new
@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

def edit
end

def update
if @forum_thread.update forum_thread_params
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])
end
Forum Posts Controller

class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread

def new
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user
end

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

def edit
end

def update
if @forum_post.update(forum_post_params)
redirect_to @forum_thread
else
render 'edit'
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
log

I, [2015-11-18T06:16:17.418239 #16523] INFO -- : Started GET "/forum/forum_threads/17" for 24.220.125.144 at 2015-11-18 06:16:17 -0600
I, [2015-11-18T06:16:17.453154 #16523] INFO -- : Processing by ForumThreadsController#show as HTML
I, [2015-11-18T06:16:17.453292 #16523] INFO -- : Parameters: {"id"=>"17"}
D, [2015-11-18T06:16:17.479120 #16523] DEBUG -- : [[1m[[36mForumThread Load (0.5ms)[[0m [[1mSELECT forum_threads.* FROM forum_threads WHERE forum_threads.id = $
D, [2015-11-18T06:16:17.487690 #16523] DEBUG -- : [[1m[[35mCACHE (0.0ms)[[0m SELECT forum_threads.* FROM forum_threads WHERE forum_threads.id = 17 LIMIT 1 [["i$
D, [2015-11-18T06:16:17.503484 #16523] DEBUG -- : [[1m[[36mUser Load (0.9ms)[[0m [[1mSELECT users.* FROM users WHERE users.deleted_at IS NULL AND users.id $
D, [2015-11-18T06:16:17.541778 #16523] DEBUG -- : [[1m[[35mUser Load (0.5ms)[[0m SELECT users.* FROM users WHERE users.deleted_at IS NULL AND users.id = 9 L$
D, [2015-11-18T06:16:17.573968 #16523] DEBUG -- : [[1m[[36mForumPost Load (0.6ms)[[0m [[1mSELECT forum_posts.* FROM forum_posts WHERE forum_posts.forum_thread_id$
D, [2015-11-18T06:16:17.579587 #16523] DEBUG -- : ^[[1m^[[35mCACHE (0.0ms)^[[0m SELECT
users.* FROMusersWHEREusers.deleted_atIS NULL ANDusers.id= 9 LIMIT$
I, [2015-11-18T06:16:17.583134 #16523] INFO -- : Rendered forum_posts/_forum_post.html.erb (7.0ms)
I, [2015-11-18T06:16:17.583329 #16523] INFO -- : Rendered forum_threads/show.html.erb within layouts/application (48.1ms)
I, [2015-11-18T06:16:17.583711 #16523] INFO -- : Completed 500 Internal Server Error in 130ms (ActiveRecord: 6.8ms)
F, [2015-11-18T06:16:17.585545 #16523] FATAL -- :
ActionView::Template::Error (undefined method
email' for nil:NilClass):
1: <%= div_for forum_post do %>
2:

<%= image_tag current_user.gravatar_url(:size => 65), :class => "img-circle avatar" %><$
3:

4:

Posted by <%= forum_post.user.email %> <%= local_time_ago forum_post.created_at %>


5:

<%= forum_post.body %>


6:

7: <% end %>
app/views/forum_posts/forum_post.html.erb:4:in `block in _app_views_forum_postsforum_post_html_erb2875746267982681747_45837340'
app/views/forum_posts/_forum_post.html.erb:1:in `_app_views_forum_posts
forum_post_html_erb_2875746267982681747_45837340'
app/views/forum_threads/show.html.erb:6:in `_app_views_forum_threads_show_html_erb
3506623455216328957_43396520'

It sounds like your forum_post.user.email isn't working. Your forum_post must not have had it's user_id set in the database. It looks like your controller is setting it properly, but you should check it out in the console to make sure it's actually saving it.

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.