質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2118閲覧

ActiveModel::UnknownAttributeError: unknown attribute 'admin' for User.となり初期データを登録できない

rieru

総合スコア6

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/12/03 14:03

編集2019/12/05 00:14

前提・実現したいこと

ここに質問の内容を詳しく書いてください。
(例)PHP(CakePHP)で●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。

User.create!(name: 'admin', email: 'admin@example.com', password: 'password', password_confirmation: 'password', admin: true)

発生している問題・エラーメッセージ

terminal

1ActiveModel::UnknownAttributeError: unknown attribute 'admin' for User. 2from /Users/user/rails/taskleaf/vendor/bundle/ruby/2.6.0/gems/activemodel-5.2.3/lib/active_model/attribute_assignment.rb:53:in `_assign_attribute'

該当のソースコード

Rails.application.routes.draw do namespace :admin do resources :users end root to: 'tasks#index' resources :tasks get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' end

app/controllers/admin/users_contriller.rb

class Admin::UsersController < ApplicationController before_action :require_admin def new @user = User.new end def edit @user = User.find(params[:id]) end def show @user = User.find(params[:id]) end def index @users = User.all end def create @user = User.new(user_params) if @user.save redirect_to admin_user_url(@user), notice: "ユーザー「#{@user.name}」を登録しました。" else render :new end end def update @user = User.find(params[:id]) if @user.update(user_params) redirect_to admin_user_url(@user), notice: "ユーザー「#{@user.name}」を更新しました。" else render :edit end end def destroy @user = User.find(params[:id]) @user.destroy redirect_to admin_user_url, notice: "ユーザー「#{user.name}」を削除しました。" end private def user_params params.require(:user).permit(:name, :email, :admin, :password, :password_confirmation) end def require_admin redirect_to root_url, unless current_user.admin? end end

schema.rb

# 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: 2019_11_28_035704) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "tasks", force: :cascade do |t| t.string "name", limit: 30 t.text "description" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "user_id", null: false t.index ["user_id"], name: "index_tasks_on_user_id" end create_table "users", force: :cascade do |t| t.string "name", null: false t.string "email", null: false t.string "password_digest", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email"], name: "index_users_on_email", unique: true end end

試したこと

$ bundle list | grep bcrypt * bcrypt (3.1.13)

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

peperoncino000

2019/12/04 01:19

usersテーブルにadminというカラムはありますか?
rieru

2019/12/04 23:58

pry(main)> User.column_adminでusersテーブルにadminカラムがあるか確認したところ User.column_admin NoMethodError: undefined method `column_admin' for User (call 'User.connection' to establish a connection):Class とエラーが出ました。 add_admin_to_users.rbに`add_column :users, :admin, :boolean, default: false, null: false`の記述はあるのですがadminフラグが追加されていなかったということでしょうか。
peperoncino000

2019/12/05 00:01

db:migrateしましたか? schema.rbはどうなっていますか?
rieru

2019/12/05 00:16

何度もすみません。db:migrateはマイグレーションファイルの編集後に行いました。`schema.rb`を追加で記載しました。`admin`が見当たりませんでした。
peperoncino000

2019/12/05 00:20

db:migrateを実行した後にadd_admin_to_users.rbを修正して再度db:migrateを実行したんですか?
guest

回答1

0

ベストアンサー

bin/rails db:rollbackadd_admin_to_users.rbの反映前に戻してからbin/rails db:migrateを実行してください。

投稿2019/12/05 00:28

peperoncino000

総合スコア148

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

rieru

2019/12/05 05:55

ご丁寧にありがとうございます。ご指摘通り、`bin/rails db:rollback` で`add_admin_to_users.rb`の反映前に戻してから`bin/rails db:migrate`を実行したのですがadminカラムを作成できていませんでした。 再度マイグレーションファイルを作成している段階で気づいたのですが、(https://joppot.info/2014/05/23/1430)「def changeの中に加える内容が書かれていない場合はrailsのコマンドが間違っています。」のとおり、カラム作成時のrailsコマンドが間違っていたようです。`rails generate migration AddAdminToUsers tag:boolean`でadminカラムを作成し、adminユーザーのデータを作成できました。 アドバイス頂き気づくことができました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問