2016-08-18 4 views
-1

と工夫を使用しようとしているNomethoderror:私が考案のガイドを次のようだし、私は現在、これで立ち往生していますレール

`NoMethodError in TasksController#new undefined method `tasks' for nil:NilClass 

Task.rb

class Task < ActiveRecord::Base 
    belongs_to :user 
    end 

User.rb

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_many :tasks 
end 

TaskController.rb

def new 
    @task = current_user.tasks.build 
end 

def create 
    @task = current_user.tasks.build(task_params) 
end 

DB - Schema.rb

create_table "tasks", force: :cascade do |t| 
    t.string "name" 
    t.text  "content" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "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.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
end 

ヘルプははるかに高く評価されます!

+0

あなたは認証されていますか? – Makoto

+0

どのように認証されましたか? – Drixzy

+1

あなたは絶対に正しく、ありがとうHasmukh! – Drixzy

答えて

0

エラーメッセージは言う:nil.tasks

それは、Userクラスオブジェクトが作成されていないことを意味します。

ログインしてください。ログインすると、エラーが解決されます。

関連する問題