RailsTutorial 13章のデータベースモデルに関する質問です。
以下のようなページから、ユーザーの削除を実行した際に、
次のようなエラーメッセージが表示されてしまいます。
この解決策・原因のいずれかについて、助言をいただきたいです。
Error: UsersIndexTest#test_index_as_admin_including_pagination_and_delete_links: NoMethodError: undefined method `picture_will_change!' for #<User:0x00000000067e97e8> Did you mean? picture_cache app/controllers/users_controller.rb:46:in `destroy'
require 'test_helper' class UsersIndexTest < ActionDispatch::IntegrationTest def setup @admin = users(:michael) @non_admin = users(:archer) end test 'index as admin including pagination and delete links' do log_in_as(@admin) get users_path assert_template 'users/index' assert_select 'div.pagination' first_page_of_users = User.paginate(page: 1) first_page_of_users.each do |user| assert_select 'a[href=?]', user_path(user), text: user.name unless user == @admin assert_select 'a[href=?]', user_path(user), text: 'delete' end end assert_difference 'User.count', -1 do delete user_path(@non_admin) end end end
class UsersController < ApplicationController before_action :logged_in_user, only: [:edit, :update, :index, :destroy, :following, :followers] before_action :correct_user, only: [:edit, :update] before_action :admin_user, only: :destroy def show @user = User.find(params[:id]) @microposts = @user.microposts.paginate(page: params[:page]) redirect_to root_url and return unless @user.activated == true end def index @users = User.where(activated: true).paginate(page: params[:page]) end def new @user = User.new end def create @user = User.new(user_params) if @user.save @user.send_activation_email flash[:info] = 'Please check your emial to activate your account' redirect_to root_url else render 'new' end end def edit end def update if @user.update_attributes(user_params) flash[:success] = "Profile updated" redirect_to @user else render 'edit' end end def destroy User.find(params[:id]).destroy flash[:success] = "User deleted" redirect_to users_url end def following @title = 'Following' @user = User.find(params[:id]) @users = @user.following.paginate(page: params[:page]) render 'show_follow' end def followers @title = 'Followers' @user = User.find(params[:id]) @users = @user.followers.paginate(page: params[:page]) render 'show_follow' end private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end def correct_user @user = User.find(params[:id]) redirect_to(root_url) unless current_user?(@user) end def admin_user redirect_to(root_url) unless current_user.admin? end end
また、UserモデルとMicropostモデルは以下の様に紐づけております。
User.rb
1class User < ApplicationRecord 2 3 has_many :microposts, dependent: :destroy 4 5 mount_uploader :picture, PictureUploader 6(中略) 7 8end
class Micropost < ApplicationRecord belongs_to :user default_scope -> { order(created_at: :desc) } mount_uploader :picture, PictureUploader validates :user_id, presence: true validates :content, presence: true, length: { maximum: 140 } validate :picture_size private def picture_size if picture.size > 5.megabytes errors.add(:picture, 'should be less than 5MB') end end end
また、以下のマイグレーションも実行済みです。
class CreateMicroposts < ActiveRecord::Migration[5.0] def change create_table :microposts do |t| t.text :content t.references :user, foreign_key: true t.timestamps end add_index :microposts, [:user_id, :created_at] end end
また、以下のマイグレーションを実行しており、コンソールでは以下の表示を得られています。
class AddPictureToMicroposts < ActiveRecord::Migration[5.1] def change add_column :microposts, :picture, :string end end
ec2-user:~/environment/ap (master) $ rails c test Running via Spring preloader in process 4052 Loading test environment (Rails 5.1.4) 2.6.3 :001 > micropost = Micropost.first Micropost Load (1.9ms) SELECT "microposts".* FROM "microposts" ORDER BY "microposts"."created_at" DESC LIMIT ? [["LIMIT", 1]] => #<Micropost id: 941832919, content: "Writing a short test", user_id: 762146111, created_at: "2019-10-06 04:14:25", updated_at: "2019-10-06 04:14:25", picture: nil> 2.6.3 :002 > user = User.first User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]] => #<User id: 14035331, name: "User 19", email: "user-19@example.com", created_at: "2019-10-06 04:14:25", updated_at: "2019-10-06 04:14:25", password_digest: "$2a$04$n8A5cjRLPSUv./2tCFaC4eKBRXxDmaGryv/FJn6AXMG...", remember_digest: nil, admin: false, activation_digest: nil, activated: true, activated_at: "2019-10-06 04:14:25", reset_digest: nil, reset_sent_at: nil> 2.6.3 :003 >
RailsTutorialをやったわけじゃないですが、エラーメッセージが指摘している
app/controllers/users_controller.rb:46:in `destroy'
と、貼っていただいているUsersControllerは同一のものでしょうか?(46行もありません)
また、エラーメッセージはテストプログラムでエラーになっていると思われます。
画面からの操作でテストが走るとは思えないので正確なusers_controller.rbの中身を貼ってもらえればと思います。
(もしくはエラーメッセージの貼り間違えをされているかも?)
https://railstutorial.jp/chapters/user_microposts?version=5.0#cha-user_microposts
見ましたが、
$ rails generate migration add_picture_to_microposts picture:string
$ rails db:migrate
を実行しています。
マイグレーションファイルにpictureがありませんが、本当にマイグレーションされていますか?
あなたの回答
tips
プレビュー