webアプリケーションを作っているのですが、バリデーション機能をつけ、エラーメッセージを表示させるコードを書いたのですがエラー文が出て、うまくページに表示されません。ページはエラー起きた時の方に移動されているのでバリデーション自体はうまく行っていると思います。
ページを開くとこのようなエラーが出ます。
railsを始めたばかりで、わからないことも多いですが、アドバイスいただけると嬉しいです。
index.html.erb
<div class="container"> <% if @book.errors.any? %> <div id="error_explanation" class="alert alert-danger"> <ul> <% @book.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="row mt-4"> <div class="col-3"> <div class="col"> <h1>User info</h1> <table class="table"> <thead> <tr> <th><%= attachment_image_tag(@user, :profile_image, :fill, 100, 100, fallback: "no_image.jpg") %></th> <th></th> </tr> </thead> <tbody> <tr> <td>name</td> <td><%= @user.name %></td> </tr> <tr> <td>introduction</td> <td><%= @user.introduction %></td> </tr> </tbody> </table> </div> <div class="col"> <div class="profile-edit text-center"> <%= link_to edit_user_path(@user), method: :get do %> <p class="border border-dark rounded" style="color:black;"><i class="far fa-user"></i></p> <% end %> </div> </div> <div class="col"> <h1>New book</h1> <div class="user-show-form"> <%= form_for (@book_new) do |f| %> <div class="show-form-title">Title</div> <div class="show-form-title2"><%= f.text_field :title %></div> <div class="show-form-opinion">Opinion</div> <div class="show-form-opinion2"><%= f.text_area :body %></div> <div class="show-form-submit"><%= f.submit 'Create Book' %></div> <% end %> </div> </div> </div> <div class="col-8"> <div class="user-index-title"> <h1>users</h1> </div> <table class="table"> <thead> <tr> <th>image</th> <th>name</th> <th></th> </tr> </thead> <% @books.each do |book| %> <tbody> <td><%= attachment_image_tag book.user, :profile_image, :fill, 100, 100, fallback: "no_image.jpg" %></td> <td> <%= link_to book_path(book.id), method: :get do %> <%= book.title %> <% end %> </td> <td> <%= book.body %> </td> </tbody> <% end %> </table> </div> </div> </div>
book.rb
class Book < ApplicationRecord belongs_to :user validates :title, presence: true validates :body, presence: true,length: { maximum: 75 } end
books.contoller.rb
class BooksController < ApplicationController def index @books = Book.all @user = current_user @book_new = Book.new end def show @book = Book.find(params[:id]) @user = User.find(@book.user_id) @book_new = Book.new end def new end def create @book = Book.new(list_params) @book.user_id = current_user.id if @book.save redirect_to book_path(@book.id) else redirect_to books_path end end def delete @book = Book.find(params[:id]) if @book.destroy redirect_to books_index_path end end def edit end private def list_params params.require(:book).permit(:title, :body,:user_id) 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: 2021_03_06_142649) do create_table "books", force: :cascade do |t| t.string "title" t.string "body" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.string "name" t.string "introduction" t.string "profile_image_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end
試したこと
・コードの確認
・エラー文の確認
お忙しいとは思いますが「、よろしくお願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/03/11 15:48
2021/03/11 15:55
2021/03/12 10:13