我创建了一个应用程序,用户可以创建一个主题,其他人可以发布他们对该主题的评论。我现在可以创建一个主题并 post.form 工作,但是当我发表评论时,我得到
NoMethodError in Posts#create
undefined method `each' for nil:NilClass
Extracted source (around line #2):
1:
2: <% for topic in @topics do %>
3: <li><%=link_to topic.title, topic_path(topic) %></li>
4:
5: <%= will_paginate @topics %>
我的帖子控制器是:
class PostsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def create
@post = current_user.posts.build(params[:post])
if @post.save
flash[:success] = "Konu oluşturuldu!"
redirect_to topic_path(topic)
else
render 'static_pages/home'
end
end
def destroy
@post.destroy
redirect_to root_path
end
private
def correct_user
@post = current_user.posts.find_by_id(params[:id])
redirect_to root_path if @post.nil?
end
end
当我创建一个帖子时,它会重定向到 localhost/posts ......虽然我希望它留在那个主题中。我怎样才能正确重定向?