关联显示nil的未定义方法`posts':NilClass

时间:2014-11-08 18:32:38

标签: ruby-on-rails

我的rails应用程序中有3个模型,我试图将它们关联起来

使用设备

创建的用户模型

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_one :userdetail

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

这是我的主题模型

class Topic < ActiveRecord::Base
  #relation between topics and post
  has_many :posts
end

这是我的帖子模型

class Post < ActiveRecord::Base
  #relation between topics and post
  belongs_to :topic

  belongs_to :user

  #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 PostsController < ApplicationController
 # before_action :set_post, only: [:show, :edit, :update, :destroy]

  before_filter :has_userdetail_and_topic, :only =>[:new, :create]



  # GET /posts
  # GET /posts.json

  #for new association SAAS book

  protected
  def has_userdetail_and_topic

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

  public

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

  end

  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
  end

  # GET /posts/new


  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create


    @current_user.posts << @topic.posts.build(params[:post])

    #@topic.posts << @post

    #@post = Post.new(post_params )

    #@post.userdetail_id = current_user.id

     #Association functional between topic and post
     #Class variable used
     #@@topic.posts << @post

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    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 scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:topic_id,:issue, :description, :rating, :user_id)
    end
end

这是我发布的新视图

<%= form_tag topic_posts_path(@topic) do  %>
    <%= label :post, :description %>
<%= text_area :post, :description %>

    <%= submit_tag 'Create post' %>

<%end%>

我在post controller中收到此错误。我不知道为什么会收到此错误

未定义的方法`posts'为nil:NilClass为@ current_user.posts&lt;&lt; @ topic.posts.build(PARAMS [:交])

我正在尝试关联主题和帖子 在我的主题的节目视图中。

我使用了代码&lt;%= link_to'Write',new_topic_post_path(topic_id:@topic)%&gt;

<%= link_to 'Write', new_topic_post_path(topic_id: @topic) %>

我正在使用Device进行用户登录,我没有在我的应用中的任何地方设置current_user。

我想创建一个用户可以在主题上创建帖子的应用。 每个主题都有多个帖子。 我是新人,学习RoR所以请解释答案

1 个答案:

答案 0 :(得分:1)

设备为您提供current_user帮助程序(不含@)。使用它,而不是@current_user

您可能还想设置

before_filter :authenticate_user

在您的帖子PostsController中 - 确保current_user没有(并且用户已正确登录)。

还应更改create方法的一部分:

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