【问题标题】:Nomethoderror trying to use Devise with railsNomethoderror 尝试将 Devise 与 rails 一起使用
【发布时间】:2016-08-18 23:11:28
【问题描述】:

我正在关注有关设计的指南,但我目前对此感到困惑:

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

任务.rb

  class Task < ActiveRecord::Base
    belongs_to :user
  end

用户.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

任务控制器.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

非常感谢您的帮助!

【问题讨论】:

  • 你认证了吗?
  • 如何认证?
  • 你完全正确,非常感谢 Hasmukh!
  • @Drixzy,太好了,您可以接受它作为答案。

标签: ruby-on-rails ruby devise


【解决方案1】:

错误信息说:nil.tasks

这意味着User 类对象没有被创建。

尝试登录。登录后,错误将得到解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 2018-06-08
    • 2016-03-25
    • 2013-12-25
    • 2014-09-01
    相关资源
    最近更新 更多