具有嵌套属性的待办事项列表

时间:2017-04-04 04:21:20

标签: ruby-on-rails devise

更新:所以经过近24小时的尝试,我终于找到了问题所在。那是在模型(reject_of :: new_record?),我打算拒绝空白,但现在我看到它拒绝新的记录。因此,每次我创建一个新实例时,List的模型都拒绝它。

所以我试图创建一个ToDo应用程序,其中包含用户,待办事项列表和待办事项。我使用设计使用户登录/登录,电子邮件确认等。因此,列表具有项目的嵌套属性,因为两者将以相同的形式同时创建。继续在Ill上使论坛充满活力。事情是当我尝试创建它时给我这个错误:未定义的方法`user_id ='为nil:NilClass。我做错了什么?

我的控制器和型号如下:

class ListsController < ApplicationController

  before_action :set_list, except: [:index, :new, :create]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @lists = List.all
    @users = User.all
  end

  def show

  end

  def new
    @list = List.new
    @list.todos.new
  end

  def edit
  end

  def create
    @list = current_user.lists.new(list_params)
    @list.todos.first.user_id = current_user.id

    if @list.save
        redirect_to @list, notice: "List was successfuly created!"
    else
        render action: :new
    end
  end

  def update

    if @list.update(list_params)
     redirect_to @list, notice: "List was successfuly updated!"
    else
     render action: :edit
    end
  end

  def destroy
    @list.destroy
    redirect_to lists_url, notice: "List was successfuly destroyed!"
  end

  def is_closed?
    @list.close
  end

  private

  def set_list
    @list = List.find(params[:id])
  end

  def list_params
    params.require(:list).permit(:title, :public, :close, todos_attributes: [:list_id, :user_id, :task, :close]  )
  end

end

class User < ApplicationRecord


  has_many :lists, dependent: :destroy
  has_many :todos, dependent: :destroy


  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :lockable, :timeoutable, :confirmable
end

class Todo < ApplicationRecord
    belongs_to :list, inverse_of: :todos
    belongs_to :user

    validates :task, presence: :true
end

class List < ApplicationRecord
    belongs_to :user
    has_many :todos, inverse_of: :list, dependent: :destroy
    accepts_nested_attributes_for :todos, reject_if: :new_record?, allow_destroy: true

    validates :title, presence: true
    validates_associated :todos
end

我的架构:

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

  create_table "lists", force: :cascade do |t|
    t.string   "title"
    t.boolean  "public"
    t.boolean  "close"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.index ["user_id"], name: "index_lists_on_user_id"
  end

  create_table "todos", force: :cascade do |t|
    t.boolean  "close"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "list_id"
    t.text     "task"
    t.integer  "user_id"
    t.index ["list_id"], name: "index_todos_on_list_id"
    t.index ["user_id"], name: "index_todos_on_user_id"
  end

  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.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "unconfirmed_email"
    t.integer  "failed_attempts",        default: 0,  null: false
    t.string   "unlock_token"
    t.datetime "locked_at"
    t.string   "name"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
  end

end

3 个答案:

答案 0 :(得分:0)

从您的create方法中删除以下行:

@list.todos.first.user_id = current_user.id

实际上,当您创建任何模型的new实例时,它不会将任何id分配给新创建的对象。因此,此行中的@list = current_user.lists.new(list_params)新创建的@list对象没有id。因此,对于下一行,它无法找到与todos

关联的@list

答案 1 :(得分:0)

从您的create方法中删除以下行:

@ list.todos.first.user_id = current_user.id

取而代之的是使用以下内容:

@ list.user_id = current_user.id

答案 2 :(得分:0)

更新:所以经过近24小时的尝试,我终于找到了问题。那是在模型(reject_of :: new_record?),我打算拒绝空白,但现在我看到它拒绝新的记录。因此,每次我创建一个新实例时,List的模型都拒绝它。