mmfkzwtty0505
@mmfkzwtty0505

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Rspec実行後のエラーについて

解決したいこと

Rspec後にエラーが表示されました。
どのように解決すれば良いか教えていただきたいです。

  バリデーションのテスト
    titleカラム
[31m      空欄でないこと (FAILED - 1)[0m
    bodyカラム
[31m      空欄でないこと (FAILED - 2)[0m
[31m      200文字以下であること: 200文字は〇 (FAILED - 3)[0m
[31m      200文字以下であること: 201文字は× (FAILED - 4)[0m
  アソシエーションのテスト
    Userモデルとの関係
[32m      N:1となっている[0m

該当するソースコード

models/book.rb
class Book < ApplicationRecord
  belongs_to :user
  validates:title,
    presence:true
  validates:body,
    presence:true,
    length:{maximum:200}
end
class CreateBooks < ActiveRecord::Migration[6.1]
  def change
    create_table :books do |t|
      t.string :title
      t.string :body
      t.integer :user_id
      t.timestamps
    end
  end
end

controllers/books_controller.rb
class BooksController < ApplicationController

  before_action :ensure_current_user,{only:[:edit,:update]}

  def ensure_current_user
    @book = Book.find(params[:id])
    if @book.user_id != current_user.id
      redirect_to books_path
    end
  end

  def edit
    if current_user
      @book = Book.find(params[:id])
    end
  end

  def index
    @books=Book.all
    @book=Book.new
    @user=current_user
  end

  def show
    @book=Book.find(params[:id])
    @user=@book.user
    @book_new=Book.new
  end

  def create
    @book = Book.new(book_params)
    @book.user_id = current_user.id
    if @book.save
      redirect_to book_path(@book.id)
      flash[:success] = "You have created the book successfully."
    else
      @books = Book.all
      @user = current_user
      render :index
    end
  end

  def update
    @book=Book.find(params[:id])
    if @book.update(book_params)
      redirect_to book_path(@book.id)
      flash[:success]="You have created book successfully."
    else
      render:edit
    end
  end

  def destroy
    @book=Book.find(params[:id])
    @book.destroy
    redirect_to books_path
  end

  private

  def book_params
    params.require(:book).permit(:title, :body)
  end
end
0

No Answers yet.

Your answer might help someone💌