2017-04-04 11 views
0

更新:ほとんど24時間試した結果、私はついにこの問題を発見しました。これは空白を拒否しようとしていたモデル(reject_of :: new_record?)でしたが、今は新しいレコードを拒否しています。だから私は新しいインスタンスを作成するたびにListのモデルはそれを拒否していました。ネストされた属性のリストを作成する

だから、ユーザー、TodoリストとTodoアイテムを持つToDoアプリケーションを作成しようとしています。私はユーザーのログイン/ログを作成するためにdeviseを使用し、確認などを電子メールで送信するので、両方のアイテムが同じフォームで同時に作成されるため、リストにはアイテムのネストされた属性があります。さらに私は、フォーラムを動的にする。私が作成しようとすると、このエラーが発生します:未定義のメソッド `user_id = 'for 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 
+0

'create'メソッドでは、' @list'は新しいオブジェクトであり、 '@list.todos'は空になります。だから、あなたが 'ファースト'を呼ぶと、それはゼロになります。これがエラーの原因です。 – Sajin

+0

@Aram 試してみてください list.save list.todos.first.user_id = current_user.id redirect_to list、notice: "リストが正常に作成されました!" else – Vishal

+0

@Vishalありがとうございます。まだ同じ問題があります – Aram

答えて

0

UPDATE @

:そう、ほぼしよう24hsた後、私は最終的に問題を発見しました。これは空白を拒否しようとしていたモデル(reject_of :: new_record?)でしたが、今は新しいレコードを拒否しています。だから私は新しいインスタンスを作成するたびにListのモデルはそれを拒否していました。

0

あなたの作成方法から、次の行を削除します。実際に

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

を、任意のmoのインスタンスnewを作成するとdelは新しく作成されたオブジェクトにidを割り当てません。したがって、この行の@list = current_user.lists.new(list_params)は、新しく作成された@listオブジェクトにはidがありません。 list.todos.first.user_id = current_user.id

@

代わりに、次を使用しますので、次の行のためには、以下の行を削除し、あなたの作成方法から@list

0

に関連付けられているtodosを見つけることができません:list.user_id = current_user.id

関連する問題