Ask A Question

Notifications

You’re not receiving notifications from this thread.

Devise no current_user data after update

Erick Sitter asked in Gems / Libraries

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 SELECTusers.* 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 methodemail' 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_forumpostsforum_post_html_erb2875746267982681747_45837340'
app/views/forum_posts/forum_post.html.erb:1:in
app_views_forum_postsforum_post_html_erb_2875746267982681747_45837340'
app/views/forum_threads/show.html.erb:6:in `app_views_forum_threadsshow_html_erb3506623455216328957_43396520'

when I make edit's it comes up as Posted by datetime, does not even display #User:0x0000000579c610,
I tried passing @forum_user.post

Reply

Hey Erick, did you get this figured out? I was doing a bit of traveling for the holidays and missed your post.

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.