【问题标题】:Association showing undefined method `posts' for nil:NilClass显示 nil:NilClass 的未定义方法“posts”的关联
【发布时间】:2014-11-08 18:32:37
【问题描述】:

我的 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%>

我在后期控制器中收到此错误。我不确定为什么会收到此错误

nil 的未定义方法 `posts':@current_user.posts 的 NilClass

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

我使用了代码

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

我正在使用设备进行用户登录,但我没有在我的应用程序的任何地方设置 current_user。

我想创建一个应用程序,用户可以在其中创建有关主题的帖子。 每个主题都有多个帖子。 我是新手,正在学习 RoR,所以请解释答案

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    设备为您提供current_user 助手(没有@)。使用它,而不是 @current_user

    您可能还想设置

    before_filter :authenticate_user
    

    在您的帖子PostsController - 确保current_user 不为零(并且用户已正确登录)。

    create 方法的一部分也应该改变:

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

    【讨论】:

    • 现在显示 ActiveModel::ForbiddenAttributesError
    • stackoverflow.com/questions/17335329/… 你可能想阅读一些 Rails 教程...
    • params.require(:post).permit(:topic_id,:issue, :description, :rating, :user_id) 有问题吗
    • 你正在使用:params[:post] 你应该使用 post_params 来代替
    • 我理解那个帖子,但无法判断我哪里出错了
    猜你喜欢
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    相关资源
    最近更新 更多