首页 文章

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

提问于
浏览
0

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

我用Device创建的用户模型

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 << @ topic.posts.build(params [:post])

我想在主题的节目视图中关联主题和帖子 .

我使用代码<%= link_to'Write',new_topic_post_path(topic_id:@topic)%>

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

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

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

1 回答

  • 1

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

    您可能还想设置

    before_filter :authenticate_user
    

    在您的帖子 PostsController 中 - 确保 current_user 不是nil(并且用户已正确登录) .

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

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

相关问题