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

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

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

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 5

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

Ruby

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

Q&A

0回答

1219閲覧

NotNullViolation: SQLite3::ConstraintException を解決したい

sakana-suki

総合スコア14

Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 5

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

Ruby

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

0グッド

1クリップ

投稿2019/09/10 02:49

編集2022/01/12 10:55

rails test を実行すると出てくるエラーの解決法がわかりません。
どこをどう直せばいいのでしょうか。どなたか教えていただけると幸いです。

やろうとしていること

アドミンではないユーザーがアドミン属性をwebからいじれないかどうかを確かめるテストをしようとしています。
テストコード自体はrailsチュートリアルの通りです。

ユーザーやユーザーの属性に関してテストをやろうとしているのに、何故photos.imageでのエラーが出るんだろう??というレベルです。。すみません。。

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

Error: UsersControllerTest#test_should_not_allow_the_admin_attribute_to_be_edited_via_the_web: ActiveRecord::NotNullViolation: SQLite3::ConstraintException: photos.image may not be NULL: INSERT INTO "photos" ("created_at", "updated_at", "id") VALUES ('2019-09-10 01:49:00.601772', '2019-09-10 01:49:00.601772', 980190962) bin/rails test test/controllers/users_controller_test.rb:10

該当のソースコード

↓ /insta-clone/test/controllers/users_controller_test.rb

require 'test_helper' class UsersControllerTest < ActionDispatch::IntegrationTest def setup @user = users(:michael) @other_user = users(:archer) end test "should not allow the admin attribute to be edited via the web" do log_in_as(@other_user) assert_not @other_user.admin? patch user_path(@other_user), params: { user: { password: @other_user.password, password_confirmation: @other_user.password, admin: true } } assert_not @other_user.reload.admin? end end

フィクスチャ
/insta-clone/test/fixtures/users.yml

michael: name: Michael Example email: michael@example.com password_digest: <%= User.digest('password') %> admin: true archer: name: Sterling Archer email: duchess@example.gov password_digest: <%= User.digest('password') %>

nullと言われている photos.image のphotoモデルです。
/insta-clone/app/models/photo.rb

class Photo < ApplicationRecord validates :image, presence: true belongs_to :post mount_uploader :image, ImageUploader end

試したこと

・rails db:migrate:reset

補足情報

・rails チュートリアルの10章の10.56のテストです。

・チュートリアルの内容を所々参考にして抜き出して使用しているので、順当に最初からチュートリアルに従って作成した場合には出なかったエラーなのかもしれません。

情報の追加

/insta-clone/app/models/user.rb

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :omniauthable validates :name, presence: true, uniqueness: { case_sensitive: :false }, length: { maximum: 50 } has_many :posts, dependent: :destroy has_many :likes has_many :comments has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy has_many :following, through: :active_relationships, source: :followed has_many :followers, through: :passive_relationships, source: :follower def self.from_omniauth(auth) where(provider: auth.provider, uid: auth.uid).first_or_create do |user| user.email = auth.info.email user.password = Devise.friendly_token[0,20] user.name = auth.info.name # assuming the user model has a name user.image = auth.info.image # assuming the user model has an image # If you are using confirmable and the provider(s) you use validate emails, # uncomment the line below to skip the confirmation emails. # user.skip_confirmation! end end def update_without_current_password(params, *options) params.delete(:current_password) if params[:password].blank? && params[:password_confirmation].blank? params.delete(:password) params.delete(:password_confirmation) end result = update_attributes(params, *options) clean_up_passwords result end def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) end def follow(other_user) following << other_user end def unfollow(other_user) active_relationships.find_by(followed_id: other_user.id).destroy end def following?(other_user) following.include?(other_user) end end

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

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

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

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

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

winterboum

2019/09/10 12:47

model Userの関連関係の定義部分を見せて下さい
sakana-suki

2019/09/10 23:37

見ていただきありがとうございます。 user.rbを「情報の追加」として貼り付けてみました。 ごちゃごちゃしていてすみません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問