Open Oba-eng opened 7 months ago
テーブルがぐちゃぐちゃなのでローカルでrails db:migrate
が出来るか確認してみてください
db/schema.rb
を削除
bin/rails db:drop db:create
bin/rails db:migrate:reset
migrationファイルの順番もぐちゃぐちゃなので全て削除してmigrationファイルを一から作り直す感じが早いかもしれません
マイグレーションファイルを一から作り直してローカル環境ではmeuの作成、ユーザーの作成やログインも機能するようになりました。
ただHeroku上だと依然としてエラーになってしまいます。
2024-04-10T12:53:18.992480+00:00 heroku[router]: at=info method=GET path="/menus/new" host=curry-web-a51113044e5a.herokuapp.com request_id=b91dfb53-f096-4e6d-a375-cbfd6c35b79f fwd="60.112.160.102" dyno=web.1 connect=0ms service=7ms status=200 bytes=5434 protocol=https
2024-04-10T12:53:31.738503+00:00 app[web.1]: I, [2024-04-10T12:53:31.738436 #2] INFO -- : [60c19e6b-862c-4a71-bcb2-8219fc5e4445] Started POST "/menus" for 60.112.160.102 at 2024-04-10 12:53:31 +0000
2024-04-10T12:53:31.739135+00:00 app[web.1]: I, [2024-04-10T12:53:31.739098 #2] INFO -- : [60c19e6b-862c-4a71-bcb2-8219fc5e4445] Processing by MenusController#create as HTML
2024-04-10T12:53:31.739190+00:00 app[web.1]: I, [2024-04-10T12:53:31.739171 #2] INFO -- : [60c19e6b-862c-4a71-bcb2-8219fc5e4445] Parameters: {"utf8"=>"✓", "authenticity_token"=>"yUrTAin16cX9cDZ+TRRi/sK2hwhag9mbDpvYlhpCOSgeRX54ayqxhEtkXHeyL+D+gjmGRKRMLzYmhGOOIVeFaA==", "menu"=>{"name"=>"aaa", "material"=>"aaa", "make"=>"aaa", "point"=>"aaa", "menu_image"=>#<ActionDispatch::Http::UploadedFile:0x0000563971905e88 @tempfile=#<Tempfile:/tmp/RackMultipart20240410-2-crvi18.png>, @original_filename="1e161a0ae12dcdc03814dd3822dd26c6.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"menu[menu_image]\"; filename=\"1e161a0ae12dcdc03814dd3822dd26c6.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"登録"}
2024-04-10T12:53:31.741020+00:00 app[web.1]: D, [2024-04-10T12:53:31.740978 #2] DEBUG -- : [60c19e6b-862c-4a71-bcb2-8219fc5e4445] User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]]
2024-04-10T12:53:31.799846+00:00 app[web.1]: I, [2024-04-10T12:53:31.799743 #2] INFO -- : [60c19e6b-862c-4a71-bcb2-8219fc5e4445] Completed 500 Internal Server Error in 60ms (ActiveRecord: 0.4ms | Allocations: 4078)
2024-04-10T12:53:31.800173+00:00 app[web.1]: F, [2024-04-10T12:53:31.800149 #2] FATAL -- : [60c19e6b-862c-4a71-bcb2-8219fc5e4445]
2024-04-10T12:53:31.800174+00:00 app[web.1]: [60c19e6b-862c-4a71-bcb2-8219fc5e4445] ActiveModel::UnknownAttributeError (unknown attribute 'user_id' for Menu.):
2024-04-10T12:53:31.800175+00:00 app[web.1]: [60c19e6b-862c-4a71-bcb2-8219fc5e4445]
2024-04-10T12:53:31.800175+00:00 app[web.1]: [60c19e6b-862c-4a71-bcb2-8219fc5e4445] app/controllers/menus_controller.rb:27:in `create'
2024-04-10T12:53:31.800902+00:00 heroku[router]: at=info method=POST path="/menus" host=curry-web-a51113044e5a.herokuapp.com request_id=60c19e6b-862c-4a71-bcb2-8219fc5e4445 fwd="60.112.160.102" dyno=web.1 connect=0ms service=603ms status=500 bytes=1827 protocol=https
2024-04-10T12:53:31.995840+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=curry-web-a51113044e5a.herokuapp.com request_id=92a28f79-2a45-4642-a29c-4925d2d2cae8 fwd="60.112.160.102" dyno=web.1 connect=0ms service=1ms status=304 bytes=48 protocol=https
2024-04-10T13:06:34.607247+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=curry-web-a51113044e5a.herokuapp.com request_id=19c72873-47f0-49e3-bb79-bd9ba2f29eba fwd="60.112.160.102" dyno=web.1 connect=0ms service=1ms status=304 bytes=48 protocol=https
2024-04-10T13:06:37.397420+00:00 app[web.1]: I, [2024-04-10T13:06:37.397353 #2] INFO -- : [a822edb0-c172-484b-94d4-35de766e64af] Started POST "/menus" for 60.112.160.102 at 2024-04-10 13:06:37 +0000
2024-04-10T13:06:37.398203+00:00 app[web.1]: I, [2024-04-10T13:06:37.398160 #2] INFO -- : [a822edb0-c172-484b-94d4-35de766e64af] Processing by MenusController#create as HTML
2024-04-10T13:06:37.398284+00:00 app[web.1]: I, [2024-04-10T13:06:37.398255 #2] INFO -- : [a822edb0-c172-484b-94d4-35de766e64af] Parameters: {"utf8"=>"✓", "authenticity_token"=>"yUrTAin16cX9cDZ+TRRi/sK2hwhag9mbDpvYlhpCOSgeRX54ayqxhEtkXHeyL+D+gjmGRKRMLzYmhGOOIVeFaA==", "menu"=>{"name"=>"aaa", "material"=>"aaa", "make"=>"aaa", "point"=>"aaa", "menu_image"=>#<ActionDispatch::Http::UploadedFile:0x0000563971ba4f90 @tempfile=#<Tempfile:/tmp/RackMultipart20240410-2-jbohit.png>, @original_filename="1e161a0ae12dcdc03814dd3822dd26c6.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"menu[menu_image]\"; filename=\"1e161a0ae12dcdc03814dd3822dd26c6.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"登録"}
2024-04-10T13:06:37.424233+00:00 app[web.1]: D, [2024-04-10T13:06:37.424149 #2] DEBUG -- : [a822edb0-c172-484b-94d4-35de766e64af] User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]]
2024-04-10T13:06:37.457605+00:00 app[web.1]: I, [2024-04-10T13:06:37.457528 #2] INFO -- : [a822edb0-c172-484b-94d4-35de766e64af] Completed 500 Internal Server Error in 59ms (ActiveRecord: 11.8ms | Allocations: 5847)
2024-04-10T13:06:37.457962+00:00 app[web.1]: F, [2024-04-10T13:06:37.457917 #2] FATAL -- : [a822edb0-c172-484b-94d4-35de766e64af]
2024-04-10T13:06:37.457966+00:00 app[web.1]: [a822edb0-c172-484b-94d4-35de766e64af] ActiveModel::UnknownAttributeError (unknown attribute 'user_id' for Menu.):
2024-04-10T13:06:37.457967+00:00 app[web.1]: [a822edb0-c172-484b-94d4-35de766e64af]
2024-04-10T13:06:37.457967+00:00 app[web.1]: [a822edb0-c172-484b-94d4-35de766e64af] app/controllers/menus_controller.rb:27:in `create'
2024-04-10T13:06:37.458679+00:00 heroku[router]: at=info method=POST path="/menus" host=curry-web-a51113044e5a.herokuapp.com request_id=a822edb0-c172-484b-94d4-35de766e64af fwd="60.112.160.102" dyno=web.1 connect=0ms service=603ms status=500 bytes=1827 protocol=https
oobarikuto@MacBook-Air-2 carry_web %
チャットGPTに聞いてみたら Menuモデルにuser_idという属性が存在しないためだと言われました。 ただ、pushしたファイルを見てみるとmenuモデルにuser_idがは含まれています。
20240410125007_add_user_id_to_menus.rb
class AddUserIdToMenus < ActiveRecord::Migration[6.1]
def change
add_column :menus, :user_id, :integer
add_foreign_key :menus, :users
end
end
このマイグレーションで後からuser_idを追加したことが原因でしょうか?
質問内容・実現したいこと ローカルでサーバーを立ち上げてユーザー作成をしようとしたらエラーが発生する。
現状発生している問題・エラーメッセージ
どの処理までうまく動いているのか パラメーターの送信はできているかと思います。
該当のソースコード
user.rb def user_params params.require(:user).permit(:email, :password, :password_confirmation, :name) end end
class User < ApplicationRecord has_secure_password validates :email, uniqueness: true, presence: true authenticates_with_sorcery!
has_many :menus
お気に入り機能
has_many :favorites, dependent: :destroy has_many :favorite_menus, through: :favorites, source: :menu
def own?(object) id == object.user_id end
def favorite(menu) favorite_menus << menu end
def unfavorite(menu) favorite_menus.destroy(menu) end
def favorite?(menu) favorite_menus.include?(menu) end
def encrypted_password=(new_password) self.password_digest = BCrypt::Password.create(new_password) end
end
new.html.erb
From: /Users/oobarikuto/workspace/curry_web/-curry_web/carry_web/app/controllers/users_controller.rb:12 UsersController#create:
=> 12: if @user.save 13: session[:user_id] = @user.id 14: redirect_to menus_path, success: 'サインアップしました' 15: else 16: flash.now[:danger] = 'サインアップに失敗しました' 17: render :new 18: end 19: end
[1] pry(#)> @user
=> #<User:0x0000000110aa53f0
id: nil,
name: "aaa",
email: "aaa@example.com",
menu_image: nil,
created_at: nil,
updated_at: nil,
encrypted_password: "",
reset_password_token: nil,
reset_password_sent_at: nil,
remember_created_at: nil,
salt: nil,
password_digest: nil,
crypted_password: nil>
[2] pry(#)>