未定义的方法`posts'为nil:NilClass

时间:2015-07-08 15:20:08

标签: ruby-on-rails methods posts

我正在尝试我的第一个RoR项目,一切进展顺利,但现在我已经陷入了一些混乱,创建管理员用户应该只能做的帖子,每个帖子都有一个话题。我得到一个未定义的方法`发布'为nil:NilClass错误,无论我改变什么,我似乎无法解决它。花了几个小时和几个小时!就像我说的,我是新的,我知道事情是错的,我也改变了与其他用户问题相关的事情,但仍然无法运作,任何帮助非常感谢!谢谢!

我的管理模式:

class Admin < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, 
     :trackable, :timeoutable, :registerable, :authentication_keys => [:email]

has_many :posts
has_many :topics, :through => :posts
end

我的帖子模型:

class Post < ActiveRecord::Base
  belongs_to :admin 
  belongs_to :topic 
  default_scope -> { order(created_at: :desc) }
  validates :admin_id, presence: true
  validates :content, presence: true
  #before_action :correct_user,   only: :destroy

#post is valid only if it's associated with a topic:
validates :topic_id, :presence => true
#can also require that the referenced topic itself be valid
#in order for the post to be valid:

validates_associated :topic

  end

我的主题模型:

class Topic < ActiveRecord::Base
    has_many :posts
end

我的帖子控制器:

class PostsController < ApplicationController
  #before_action :authenticate_admin!
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_filter :has_topic, :only =>[:new, :create]

def index
  @posts = Post.all
end

def new
  @post = @topics.posts.build
  @topic = Topic.find(params[:topic_id1])
end

#Issues GET request at /posts/1
def show
  @post = Post.find(params[:id])
end

#Issues GET request at /posts/1/edit
def edit          
end    

#Issues POST request at /posts
  def create

@post = Post.new(post_params)
@post = @topic.posts.build(post_params)
@current_user.posts << @post
#@topic.posts << @post

  if @post.save
    flash[:success] = "post created!"
    redirect_to root_url
  else
    render '/'
  end
end

#Issues DELETE request /posts/1
def destroy
    @post.destroy
    flash[:success] = "Post deleted"
    redirect_to request.referrer || root_url
  end

  protected

    def has_topic
      unless(@topic =Topic.find_by_id(params[:topic_id]))
            flash[:warning] = 'post must be for an existing topic'
          end
        end

  private

# Use callbacks to share common setup or constraints between actions.
def set_post
    @post = Post.find(params[:id])
    end

# Never trust parameters from the internet, only allow the white list through. 
#:topic_id  
def post_params
     params.require(:post).permit(:id, :content, :admin_id)
end
 end

就像我之前提到的,我有一个版本的我自己的代码,它有同样的问题,我尝试通过使用另一个问题帖子中的一些代码进行调整(只是暂时 - 朋友推荐它尝试找到错误)但仍然有同样的问题。我创建了用户,管理员和帖子(通过控制台)。我也尝试将创建的一部分更改为@post = @ admin.posts.build(post_params)等,只是为了查看是否有效。我已经尝试了这么久,以至于我对这一切感到困惑,对我这么容易!非常感谢!

抱歉,我忘了说...错误来自Create方法。

@post = @topic.posts.build(post_params)
        @current_user.posts << @post

3 个答案:

答案 0 :(得分:3)

The error you're getting is that you are calling .posts on an object that is nil. Given that you call .posts on two objects in your CREATE method:

@post = @topic.posts.build(post_params)
@current_user.posts << @post

And you set @topic in a before filter, I'd suggest that @current_user is nil. The standard is to use current_user so I'd suggest removing the @ sign or setting @current_user to something.

If it's on the NEW action, you set:

@post = @topics.posts.build

Where you don't actually set @topics. Suggest that you set this to something or remove the 's' and use the @topic that you set in your before filter

答案 1 :(得分:0)

我认为错误发生在您的new方法

def new
  @post = @topics.posts.build
  @topic = Topic.find(params[:topic_id1])
end

您尚未初始化@topics,而您正在使用此行@post = @topics.posts.build

您需要将new方法更改为

def new
  @topic = Topic.find(params[:topic_id])
  @post = @topic.posts.build
end

答案 2 :(得分:0)

I'm pretty sure the error you're referencing is in your create action, where you call @post = @topic.posts... which can throw a NoMethodError because @topic is unset, but your question doesn't actually say which line is causing the error, which is a big problem for those of us trying to help you.