前提・実現したいこと
updateを正常に機能させたい。
発生している問題・エラーメッセージ
editのフォーム画面で入力をしshowの画面へ移れるが、updateが反映されない。
エラーメッセージは出ていないです。
該当のソースコード
books_controller.rb
class BooksController < ApplicationController def index @books = Book.all @book = Book.new end def create @book = Book.new(book_params) if @book.save flash[:notice] = "Book was successfully created." redirect_to book_path(book.id) else @books=Book.all render action :index end end def show @book = Book.find(params[:id]) end def edit @book = Book.find(params[:id]) end def update @book = Book.find(params[:id]) if @book.update(book_params) flash[:notice] = "Book was successfully updated." redirect_to book_path(@book) else @books=Book.all render action :edit end end def destroy book = Book.find(params[:id]) book.destroy flash[:notice] = "Book was successfully destroyed." redirect_to books_path end private def book_params params.permit(:title, :body) end end
index.html.erb
<h1>Books</h1> <table> <tr> <th> Title </th> <th>Body</th> </tr> <% @books.each do |book| %> <tr> <th> <%= book.title %> </th> <th> <%= book.body %> </th> <th> <%= link_to "show", book_path(book.id) %> </th> <th> <%= link_to "edit", edit_book_path(book.id) %> </th> <th> <%= link_to "destroy", book_path(book.id) , method: :delete %> </th> </tr> <%end%> </table> <h1>New Book</h1> <%= form_with model:@book, local:true do |f| %> <% if @book.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2> <ul> <% @book.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <h4>Title</h4> <%= f.text_field :title %> <h4>Body</h4> <%= f.text_area :body %> <%= f.submit 'Create Book' %> <% end %>
edit.html.erb
<h1>Editing Book</h1> <%= form_with model:@book, local:true do |f| %> <% if @book.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2> <ul> <% @book.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <h4>Title</h4> <%= f.text_field :title %> <h4>Body</h4> <%= f.text_area :body %> <%= f.submit 'Update Book' %> <% end %> <%= link_to "show", book_path(@book) %> <%= link_to "Back", books_path %>
show.html.erb
<p>Title: <%= @book.title %></p> <p>Body: <%= @book.body%></p> <%= link_to "Edit", edit_book_path(@book) %> <%= link_to "Back", books_path %>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/03/02 09:02