title: undefined method `posts' for nil:NilClass
body: <p>I'm trying my first RoR project and all was going well, but now I've got into a bit of a mess with creating posts that admin users are only supposed to be able to do, and each post has a topic. I'm getting a undefined method `posts' for nil:NilClass error and no matter what I change I can't seem to solve it. Spent hours and hours on it! Like I said, i'm new and I know things are wrong, I also changed things about to relate to another users questions but still doesn't function, any help much appreciated! Thanks! </p>
  
  <p>My admin model:</p>
  
  <pre><code>class Admin &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, 
       :trackable, :timeoutable, :registerable, :authentication_keys =&gt; [:email]
  
  has_many :posts
  has_many :topics, :through =&gt; :posts
  end
  </code></pre>
  
  <p>My post model:</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    belongs_to :admin 
    belongs_to :topic 
    default_scope -&gt; { 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 =&gt; true
  #can also require that the referenced topic itself be valid
  #in order for the post to be valid:
  
  validates_associated :topic
  
    end
  </code></pre>
  
  <p>My topic model:</p>
  
  <pre><code>class Topic &lt; ActiveRecord::Base
      has_many :posts
  end
  </code></pre>
  
  <p>my posts controller:</p>
  
  <pre><code>class PostsController &lt; ApplicationController
    #before_action :authenticate_admin!
    before_action :set_post, only: [:show, :edit, :update, :destroy]
    before_filter :has_topic, :only =&gt;[: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 &lt;&lt; @post
  #@topic.posts &lt;&lt; @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
  </code></pre>
  
  <p>Like I mentioned before, I had one version of all my own code, which had the same problem, I tried adapting it by using some code from another question post (just temporarily-a friend recommended it to try and find the error) but still having the same problem. I've created users, admins and posts (via console). I've also tried changing part of create to @post = @admin.posts.build(post_params) etc, just to see if anything works. I've been trying this for so long that i've just got confused with it all, go easy on me ! Many thanks!</p>
  
  <p>Sorry I forgot to say... the error comes from the Create method.</p>
  
  <pre><code>@post = @topic.posts.build(post_params)
          @current_user.posts &lt;&lt; @post
  </code></pre>