我在创建帖子时遇到问题,我似乎总是得到未定义的方法“用户名”或“电子邮件”,但是当使用current_user.username或current_user.email时,它会显示正常...
论坛主题控制器
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
论坛帖子控制器
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
日志
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`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`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: <div class="pull-avatar-left" style="padding-right: 20px; padding-bottom: 10px;"><%= image_tag current_user.gravatar_url(:size => 65), :class => "img-circle avatar" %><$
3: <div class="forum-post-container" style="margin-top: 20px; margin-left: 20px; padding-bottom: 10px;">
4: <p class="text-muted">Posted by <%= forum_post.user.email %> <%= local_time_ago forum_post.created_at %></p>
5: <p><%= forum_post.body %></p>
6: </div>
7: <% end %>
app/views/forum_posts/_forum_post.html.erb:4:in `block in _app_views_forum_posts__forum_post_html_erb___2875746267982681747_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'
答案 0 :(得分:2)
将部分_forum_post
中的代码更改为:
<%= div_for @forum_post do %>
在您的部分@
之前添加forum_post
。它没有任何错误。