简体   繁体   English

Rails 4 控制台:名称错误:未定义的局部变量或方法和未初始化的常量

[英]Rails 4 Console: Name Error: undefined local variable or method & uninitialized constant


Update: It's weird, but right now if I try to migrate something like this:更新:这很奇怪,但现在如果我尝试迁移这样的东西:

 class RemoveStuffFromTools < ActiveRecord::Migration def change remove_column :tools, :featured, :boolean remove_column :tools, :shares, :integer remove_column :tools, :views, :integer remove_column :tools, :likes, :integer remove_column :tools, :favorites, :integer end end

I get this error:我收到此错误:

 $ rails g migration remove_stuff_from_tools invoke active_record create db/migrate/20160904090608_remove_stuff_from_tools.rb Jonas@JONAS_PC ~/gitapps/ocubit (master) $ rake db:migrate == 20160904090608 RemoveStuffFromTools: migrating ============================= -- remove_column(:tools, :featured, :boolean) rake aborted! StandardError: An error has occurred, this and all later migrations canceled: undefined method `to_sym' for nil:NilClass c:/Users/Jonas/gitapps/ocubit/db/migrate/20160904090608_remove_stuff_from_tools.rb:3:in

change' c:in migrate' NoMethodError: undefined method to_sym' for nil:NilClass c:/Users/Jonas/gitapps/ocubit/db/migrate/20160904090608_remove_stuff_from_tools.rb:3:in change' c:in `migrate' Tasks: TOP => db:migrate (See full trace by running task with --trace) change' c:in migrate' NoMethodError: undefined method to_sym' for nil:NilClass c:/Users/Jonas/gitapps/ocubit/db/migrate/20160904090608_remove_stuff_from_tools.rb:3:in change' c:in `migrate' 任务:TOP => db:migrate(通过使用 --trace 运行任务查看完整跟踪)

Is this somehow related?这有什么关系吗?

I am relatively new to rails and in the process of coding an application.我对 Rails 比较陌生,并且正在编写应用程序。 So far the app itself works great.到目前为止,该应用程序本身运行良好。 Lately I wanted to take a look at my database with the Rails Console, but I received these errors:最近我想用 Rails 控制台查看我的数据库,但我收到了以下错误:

$ rails c
Loading development environment (Rails 4.2.5.1)
irb(main):001:0> Tool.last
NameError: undefined local variable or method `l' for main:Object
    from (irb):3
    from c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/console.rb:110:in `start'
    from c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/console.rb:9:in `start'
    from c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:68:in `console'
    from c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
irb(main):004:0> Tool.first
NameError: uninitialized constant T
    from (irb):5
    from c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/console.rb:110:in `start'
    from c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/console.rb:9:in `start'
    from c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:68:in `console'
    from c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
irb(main):006:0>

It is not the first time I use the Rails Console with this particular application and it worked so far.这不是我第一次将 Rails 控制台与这个特定的应用程序一起使用,并且到目前为止它工作正常。

Should I be worried, or just continue developing cause the app itself works fine?我应该担心,还是继续开发因为应用程序本身运行良好?

Here is my code:这是我的代码:
I am using Devise for the Users我正在为用户使用设计

/app/controllers/application_controller.rb /app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :exception

    before_action :configure_permitted_parameters, if: :devise_controller?

    protected

    def configure_permitted_parameters
        devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
        devise_parameter_sanitizer.permit(:account_update, keys: [:username, :phonenumber, :workplace, :website, :twitter, :linkedin, :public_email, :public_phonenumber, :description, :title, :githuburl])
    end
end

/app/controllers/tools_controller.rb /app/controllers/tools_controller.rb

class ToolsController < ApplicationController
    before_action :find_tool, only: [:show, :edit, :update, :destroy]
    before_action :authenticate_user!

    def index
        @tools = Tool.where(user_id: current_user).order("created_at DESC")
    end

    def show
    end

    def new
        @tool = current_user.tools.build
    end

    def create
        @tool = current_user.tools.build(tool_params)

        if @tool.save
            redirect_to tools_path
        else
            render 'new'
        end
    end

    def edit
    end

    def update
        if @tool.update(tool_params)
            redirect_to tools_path
        else
            render 'edit'
        end
    end

    def destroy
        @tool.destroy
        redirect_to tools_path
    end

    private

    def find_tool
        @tool = Tool.find(params[:id])
    end

    def tool_params
        params.require(:tool).permit(:title, :subtitle, :url)
    end
end

/app/models/tool.rb /app/models/tool.rb

class Tool < ActiveRecord::Base
    belongs_to :user
end

/app/models/user.rb /app/models/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 :tools

end

/db/schema.rb /db/schema.rb

# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

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

  create_table "tools", force: :cascade do |t|
    t.string   "title"
    t.string   "subtitle"
    t.string   "url"
    t.boolean  "featured"
    t.integer  "shares"
    t.integer  "views"
    t.integer  "likes"
    t.integer  "favorites"
    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
    t.string   "username"
  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

I hope the provided data is enough, if not please tell me.我希望提供的数据足够,如果没有请告诉我。 I'm grateful for all Your replies.我很感激你的所有回复。

I figured it out.我想通了。

I ran db:drop , db:create and db:migrate .我运行了db:dropdb:createdb:migrate

Now everything works fine.现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM