Rails:未定义的方法`user_id ='代表nil:NilClass

时间:2016-05-18 18:42:02

标签: ruby-on-rails ruby

我的博客中的评论控制器遇到了问题。每当我尝试创建新评论时,我都有undefined method "user_id=" for nil:NilClass,并且不会创建评论。当我试图摧毁一个时发生的情况相同,我有undefined method "comments" for #<User:0x007fe2c872f968>

当我重新启动服务器时,我创建的注释会显示,但是与destroy方法不同。

我不确定这里发生了什么,我必须对用户做错事,但我找不到问题所在。

帖子控制器

class PostsController < ApplicationController
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @posts = Post.all.order('created_at DESC')
    @posts = Post.paginate(:page => params[:page])
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)

   if  @post.save
      redirect_to @post
    else
      render "new"
    end

  end

  def show
    @post = Post.find(params[:id])
  end 

  def edit
    @post = Post.find(params[:id])
  end

  def update
    @post = Post.find(params[:id])

    if @post.update(params[:post].permit(:title, :body, :image))
      redirect_to @post
    else 
      render 'edit'
    end
  end

  def destroy
    @post = Post.find(params[:id])
    @post.destroy()

    redirect_to posts_path
  end

  private

    def post_params
      params.require(:post).permit(:title, :body, :image)
    end

end

评论控制器

class CommentsController < ApplicationController
  before_action :correct_user, only: :destroy

  def create
    @post = Post.find(params[:post_id])
    @comments = @post.comments.create(params[:comment].permit(:name, :body))
    @comment.user_id = current_user.id
    comment.save

    redirect_to post_path(@post)
  end

  def show
    @post = Post.find(params[:post_id])
    @comments = @post.comments.create(params[:comment])
  end

  def destroy
    @post = Post.find(params[:post_id])
    @comments = @post.comments.find(params[:id])
    @comments.destroy

    redirect_to post_path(@post)
  end


  private 

  def comment_params
    params.require(:commet).permit(:user_id, :name, :body)
  end

  def correct_user
    @comment = current_user.comments.find_by(id: params[:id])
      if @comment.nil?
        flash[:alert] = "Not your comment!"
        redirect_to :back
      end
  end
end

架构

ActiveRecord::Schema.define(version: 20160515190759) do

  create_table "average_caches", force: :cascade do |t|
    t.integer  "rater_id"
    t.integer  "rateable_id"
    t.string   "rateable_type"
    t.float    "avg",           null: false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "comments", force: :cascade do |t|
    t.string   "name"
    t.text     "body"
    t.integer  "post_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
  end

  add_index "comments", ["post_id"], name: "index_comments_on_post_id"

  create_table "overall_averages", force: :cascade do |t|
    t.integer  "rateable_id"
    t.string   "rateable_type"
    t.float    "overall_avg",   null: false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.text     "body"
    t.string   "image_url"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

  create_table "rates", force: :cascade do |t|
    t.integer  "rater_id"
    t.integer  "rateable_id"
    t.string   "rateable_type"
    t.float    "stars",         null: false
    t.string   "dimension"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "rates", ["rateable_id", "rateable_type"], name: "index_rates_on_rateable_id_and_rateable_type"
  add_index "rates", ["rater_id"], name: "index_rates_on_rater_id"

  create_table "rating_caches", force: :cascade do |t|
    t.integer  "cacheable_id"
    t.string   "cacheable_type"
    t.float    "avg",            null: false
    t.integer  "qty",            null: false
    t.string   "dimension"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "rating_caches", ["cacheable_id", "cacheable_type"], name: "index_rating_caches_on_cacheable_id_and_cacheable_type"

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

3 个答案:

答案 0 :(得分:2)

简单来说,您有两个不同的实例对象。此外,您已经使用 def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(comment_params.merge(user_id: current_user.id)) redirect_to post_path(@post) end 方法,只需使用该方法即可。

SQL Server

答案 1 :(得分:0)

尝试:

 @comments = @post.comments.create(params[:comment].permit(:name, :body))

只需将@comments替换为@comment

答案 2 :(得分:0)

关于您的create方法。你有两个问题:

  1. 您没有定义@comment个实例变量(您有@comments)。
  2. 您没有comment本地变量或方法。
  3. comments_controller.rb

    class CommentsController < ApplicationController
      def create
        @post = Post.find(params[:post_id])
        #       |--  HERE
        @comment = @post.comments.create(params[:comment].permit(:name, :body))
        @comment.user_id = current_user.id
        @comment.save      # <==   HERE  ==
    
        redirect_to post_path(@post)
      end
    end
    

    至于destroy,您可能忘记添加与has_many :comments模型关联的User

    class User < ActiveRecord::Base
        has_many :comments
    end
    
    class Comment < ActiveRecord::Base
        belongs_to :user
    end