前提・実現したいこと
Ruby on Railsで、「本の感想を投稿できるサイト」という名目の簡易投稿サイトを作成中です。
新規投稿時、タイトル又は本文が空白であった場合にエラーメッセージを表示させようとしたところ、下記のエラーが発生してしまいました。
丸1日調べましたが解決出来ませんでしたので、ご回答頂けると大変有り難いです。
発生している問題・エラーメッセージ
該当のソースコード
app/models/book.rb
Ruby
1class Book < ApplicationRecord 2 validates :title, presence: true 3 validates :body, presence: true 4end
app/config/routes.rb
Ruby
1Rails.application.routes.draw do 2 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 3 root to: 'homes#top' 4 get 'top' => 'homes#top' 5 get 'todolists/new' 6 post 'todolists' => 'todolists#create' 7 get 'todolists' => 'todolists#index' 8 get 'todolists/:id' => 'todolists#show',as:'todolist' 9 get 'todolists/:id/edit' => 'todolists#edit',as:'edit_todolist' 10 patch 'todolists/:id' => 'todolists#update',as:'update_todolist' 11 delete 'todolists/:id' => 'todolists#destroy',as:'destroy_todolist' 12end
app/controllers/todolists_controller.rb
Ruby
1class TodolistsController < ApplicationController 2 3 def create 4 @book = Book.new(book_params) 5 if @book.save 6 redirect_to todolist_path(@book) 7 flash[:notice] = "Book was successfully created." 8 else 9 render 'index' 10 end 11 end 12 13 def index 14 @books = Book.all 15 @book = Book.new 16 end 17 18 def show 19 @book = Book.find(params[:id]) 20 end 21 22 def edit 23 @book = Book.find(params[:id]) 24 end 25 26 def update 27 @book = Book.find(params[:id]) 28 if @book.update(book_params) 29 redirect_to todolist_path(@book) 30 flash[:notice] = "Book was successfully updated." 31 else 32 render 'edit' 33 end 34 end 35 36 def destroy 37 @book = Book.find(params[:id]) 38 @book.destroy 39 redirect_to todolists_path 40 end 41 42 private 43 def book_params 44 params.require(:book).permit(:title,:body) 45 end 46end
app/views/todolists/index.html.erb
Ruby
1<h1>Books</h1> 2 3<table> 4 <thead> 5 <tr> 6 <th>title</th> 7 <th>body</th> 8 </tr> 9 </thead> 10 11 <tbody> 12 <% @books.each do |book| %> 13 <tr> 14 <td><p><%= book.title %></p></td> 15 <td><p><%= book.body %></p></td> 16 17 <td><%= link_to "Show",todolist_path(book) %></td> 18 <td><%= link_to "Edit",edit_todolist_path(book) %></td> 19 <td><%= link_to "Destroy",destroy_todolist_path(book),method: :delete,'data-confirm' => 'Are you sure?' %></td> 20 </tr> 21 <% end %> 22 </tbody> 23</table> 24 25<h2>New Book</h2> 26 27<%= form_for(@book,url:'/todolists') do |f| %> 28 <% if @book.errors.any? %> 29 <div> 30 <h2><%= @book.errors.count %>件のエラーがあります。</h2> 31 <ul> 32 <% @book.errors.full_messages.each do |msg| %> 33 <li><%= msg %></li> 34 <% end %> 35 </ul> 36 </div> 37 <% end %> 38 <h6>Title</h6> 39 <%= f.text_field :title %> 40 41 <h6>Body</h6> 42 <%= f.text_area :body %> 43 44 <p><%= f.submit 'Create Book' %></p> 45 46<% end %>
補足情報(FW/ツールのバージョンなど)
PC : MacBook Air 2017
OS : macOS Catalina
Editer : Sublime Text3
Vagrant : 2.2.7
VirtualBox : 6.1.2
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/24 09:43
2020/02/24 12:03
2020/02/24 12:28
2020/02/25 01:28