前提・実現したいこと
投稿カラムのtitle,bodyの編集、updateができません。
エラーがなくどこが悪いのか特定できず詰まっています。
コントローラーのupdateメソッドが毎回falseでrender :editを実行しているためだと考えましたがupdateがfalseになってしまう原因がわかりません。
発生している問題・エラーメッセージ
updateできない
book_cotrollerのupdateアクション内のupdateメソッドがfalse
エラーメッセージなし
該当のソースコード
下記の他に必要なものがあればコメントもらい次第追加します。よろしくお願いします。
#books_controller.rb
class BooksController < ApplicationController before_action :set_book, only: [:show, :edit, :update, :destroy] # GET /books # GET /books.json def index @book = Book.new @books = Book.all end # GET /books/1 # GET /books/1.json def show end # GET /books/1/edit def edit end # POST /books # POST /books.json def create @book = Book.new(book_params) if @book.save redirect_to @book, notice: 'Book was successfully created.' else render :index end end # PATCH/PUT /books/1 # PATCH/PUT /books/1.json def update if @book.update(book_params) redirect_to books_path, notice: 'Book was successfully updated.' else raise @book.error render :index , notice: 'update was failed' end end # DELETE /books/1 # DELETE /books/1.json def destroy @book.destroy redirect_to books_url, notice: 'Book was successfully destroyed.' end private # Use callbacks to share common setup or constraints between actions. def set_book @book = Book.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def book_params params.require(:book).permit(:title, :body) end end
#_form.html.erb
<%= form_for(book) 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 %> <div class="field"> <%= f.label :title %> <%= f.text_field :title, class: "book_title" %> </div> <div class="field"> <%= f.label :body %> <%= f.text_area :body, class: "book_body" %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
#edit.html.erb
<h1>Editing Book</h1> <%= @book.body %> <form> <%= render 'form',{ book: @book }%> </form> <%= link_to 'Show', @book, class: "show_#{@book.id}" %> | <%= link_to 'Back', books_path, class: "back" %>
#routes.rb
Rails.application.routes.draw do resources :books # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
ログ
Started GET "/books/6/edit?utf8=%E2%9C%93&_method=patch&authenticity_token=7rGEk8CaXLQ8a7DbW3qYbwdHkVfxS%2F%2BDq1NnfuEMDu5LaXMdrdo3iCr4sCHJ4DqriEDjkXCx%2BdyAJfiZGfTCow%3D%3D&book%5Btitle%5D=title&book%5Bbody%5D=bodyaaa&commit=Update+Book" for 10.0.2.2 at 2020-06-03 12:09:24 +0000
Processing by BooksController#edit as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7rGEk8CaXLQ8a7DbW3qYbwdHkVfxS/+Dq1NnfuEMDu5LaXMdrdo3iCr4sCHJ4DqriEDjkXCx+dyAJfiZGfTCow==", "book"=>{"title"=>"title", "body"=>"bodyaaa"}, "commit"=>"Update Book", "id"=>"6"}
Book Load (0.6ms) SELECT "books".* FROM "books" WHERE "books"."id" = ? LIMIT ? [["id", 6], ["LIMIT", 1]]
Rendering books/edit.html.erb within layouts/application
Rendered books/_form.html.erb (1.9ms)
Rendered books/edit.html.erb within layouts/application (4.7ms)
Completed 200 OK in 59ms (Views: 53.5ms | ActiveRecord: 0.6ms)
補足情報(FW/ツールのバージョンなど)
Rails 5.2.4.3
回答1件
あなたの回答
tips
プレビュー