前提・実現したいこと
DBに保存したい
発生している問題・エラーメッセージ
1ページでデータの一覧と、入力フォームが表示されるようにしています。
index.etml.erb上では保存を行えます。
空データを保存した場合にエラーが出力される用のnew.etml.erb上でデータを入力すると保存が行えません。
該当のソースコード
books_controller.rb
1 def new 2 @books=Book.new 3 end 4 5 def create 6 @book=Book.new(title: params[:title],body: params[:body]) 7 if @book.save 8 flash[:notice]="Book was successfully created." 9 redirect_to book_path(@book.id) 10 else 11 @books=Book.all 12 render :new 13 end 14 end 15 16 def index 17 @books=Book.all 18 end
index.html.erb
1<html> 2 <head> 3 <meta charset="UTF-8"> 4 <link rel="stylesheet" href="books.scss"> 5 <title>index</title> 6 </head> 7 8 <body> 9 <% if flash[:notice] %> 10 <div class="flash-message"><p> <%= notice%> </p></div> 11 <% end %> 12 13 <h1>Books</h1> 14 <table> 15 <thead> 16 <tr> 17 <th>Title</th> 18 <th>Body</th> 19 <th colspan="3"></th> 20 </tr> 21 </thead> 22 23 <tbody> 24 <% @books.each do |book| %> 25 <tr> 26 <td><%= book.title %></td> 27 <td><%= book.body %></td> 28 <td><%= link_to "Show",book_path(book.id),class:"link" %></td> 29 <td><%= link_to "Edit",edit_book_path(book.id),class:"link" %></td> 30 <td><%= link_to "Destroy",destroy_list_path(@books.ids), method: :delete, data: {confirm: "削除しますか?"},class:"link" %></td> 31 </tr> 32 <% end %> 33 </tbody> 34 </table> 35 36 <h2>New book</h2> 37 38 <%= form_with model: @book, url:books_path, method: :post do |f| %> 39 <div class="field"> 40 <%= f.label :title %> 41 <%= f.text_field :title %> 42 </div> 43 <div class="field"> 44 <%= f.label :body %> 45 <%= f.text_area :body %> 46 </div> 47 <div class="actions"> 48 <%= f.submit "Create Book" %> 49 </div> 50 <% end %> 51 52 </body> 53</html>
new.html.erb
1<html> 2 <head> 3 <meta charset="UTF-8"> 4 <link rel="stylesheet" href="books.scss"> 5 <title>new</title> 6 </head> 7 8 <body> 9 <% if flash[:notice] %> 10 <div class="flash-message"><p> <%= notice%> </p></div> 11 <% end %> 12 13 <h1>Books</h1> 14 <table> 15 <thead> 16 <tr> 17 <th>Title</th> 18 <th>Body</th> 19 <th colspan="3"></th> 20 </tr> 21 </thead> 22 23 <tbody> 24 <% @books.each do |book| %> 25 <tr> 26 <td><%= book.title %></td> 27 <td><%= book.body %></td> 28 <td><%= link_to "Show",book_path(book.id),class:"link"%></td> 29 <td><%= link_to "Edit",edit_book_path(book.id),class:"link" %></td> 30 <td><%= link_to 'Destroy',book_path(book.id), method: :delete,class:"link" %></td> 31 </tr> 32 <% end %> 33 </tbody> 34 </table> 35 36 <h2>New book</h2> 37 <div class="error-message"> 38 <% if @book.errors.any? %> 39 <div class="error-title"><%= @book.errors.count %> errors prohibited this book from being saved:</div> 40 <ul> 41 <div class="error-list"> 42 <% @book.errors.full_messages.each do |message| %> 43 <li><%= message %></li> 44 <% end %> 45 </div> 46 </ul> 47 <% end %> 48 </div> 49 50 <%= form_with model: @book, url:books_path, method: :post do |f| %> 51 <div class="field"> 52 <%= f.label :title %> 53 <%= f.text_field :title %> 54 </div> 55 <div class="field"> 56 <%= f.label :body %> 57 <%= f.text_area :body %> 58 </div> 59 <div class="actions"> 60 <%= f.submit "Create Book" %> 61 </div> 62 <% end %> 63 64 </body> 65</html>
どなたか解決お願いします。
回答1件
あなたの回答
tips
プレビュー