更新 这很奇怪,但是现在如果我尝试迁移这样的东西:
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
我收到此错误:
$ 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
迁移” NoMethodError:未定义的方法to_sym' for nil:NilClass c:/Users/Jonas/gitapps/ocubit/db/migrate/20160904090608_remove_stuff_from_tools.rb:3:in
更改' c:在`migrate'中 任务:TOP =&gt; DB:迁移 (通过使用--trace运行任务查看完整跟踪)这在某种程度上是相关的吗?
我对rails和编写应用程序的过程相对较新。到目前为止,应用程序本身效果很好最近我想用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>
这不是我第一次使用Rails控制台和这个特定的应用程序,它到目前为止工作。
我应该担心,还是继续开发因为应用程序本身可以正常工作?
这是我的代码:
我正在为用户使用Devise
/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
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
class Tool < ActiveRecord::Base
belongs_to :user
end
/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
# 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
我希望提供的数据足够,如果没有请告诉我。 我很感谢你的回复。
答案 0 :(得分:-1)
我明白了。
我投放了db:drop
,db:create
和db:migrate
。
现在一切正常。
提前感谢您的帮助。