Railsの4コンソール:名エラー:未定義のローカル変数やメソッド&初期化されていない一定の
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 [email protected]_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 methodto_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)Is this somehow related?
私はレールやアプリケーションをコーディングする過程でするのは比較的新しいです。これまでのところ、アプリ自体は素晴らしい作品です。最近、私は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コンソールを使用して、それがこれまで働いていた最初の時間ではありません。
私は心配する必要がありますか、またはアプリケーション自体がうまく動作する原因を開発し続ける必要がありますか?ここで
は私のコードです:
私はユーザー
/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
/アプリ/コントローラ/ 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
提供されたd ataは十分ではない場合は教えてください。 あなたのすべての返信に感謝します。
わからない.butレール上にこれを試してみてください 「ツール」あなたがで任意のツールを作成しました –
.classify.constantize.first 「ツール」.classify.constantize.last .classify.constantize.all 「ツール」Cすべて? – luissimo
@luissimoはい、私はいくつかを作成し、それらはアプリケーションで正しく表示されています – Gugubaight