Ruby on rails - nil:NilClass 的未定义方法 'posts'



我正在尝试我的第一个RoR项目,一切都很顺利,但是现在我在创建管理用户只应该能够做的帖子时陷入了一点混乱,每个帖子都有一个主题。我得到一个未定义的方法' posts'为nil:NilClass错误,无论我改变我似乎无法解决它。花了好几个小时在上面!就像我说的,我是新来的,我知道事情是错的,我也改变了一些关于另一个用户的问题,但仍然没有功能,任何帮助非常感谢!谢谢!

My admin model:

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

My post model:

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

My topic model:

class Topic < ActiveRecord::Base
    has_many :posts
end

my posts controller:

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

你得到的错误是,你正在调用。posts上的对象是nil。假设您在CREATE方法中对两个对象调用.posts:

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

并且您在before过滤器中设置@topic,我建议@current_user为nil。标准是使用current_user,所以我建议删除@符号或将@current_user设置为其他东西。

如果它在NEW动作上,则设置:

@post = @topics.posts.build

没有设置@topics的地方。建议您将此设置为其他内容或删除's'并使用您在before过滤器

中设置的@topic

我相信错误是在你的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

我很确定你引用的错误是在你的create行动中,在那里你调用@post = @topic.posts...,它可以抛出NoMethodError,因为@topic是未设置的,但你的问题实际上并没有说哪一行是导致错误的,这对我们这些试图帮助你的人来说是一个大问题。

最新更新