RailsでCRUD処理を実行できる簡単なアプリケーションを作って練習中です。
書籍登録のアプリです。
書籍の一覧、詳細、削除は実装できましたが、編集後の更新ができません。
自分で調べてみたのですが解決法が絞れないのでみていただけないでしょうか。
以下、エディタの内容です。
###roures.rb
routes.rb
Rails.application.routes.draw do
get 'welcome/index'
For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :books
root 'welcome#index'
end
###books_controller.rb ```class BooksController < ApplicationController#to do index アクションを作る def index @books = Book.all end def show @book = Book.find(params[:id]) end def new @book = Book.new end def edit @book = Book.find(params[:id]) end def create @book = Book.new(book_params) if @book.save redirect_to @book else render :new end end def update @book = Book.find(params[:id]) if @book.update(book_params) redirect_to @book else render :edit end end def destroy @book = Book.find(params[:id]) @book.destroy! redirect_to books_path end private def book_params params.require(:book).permit(:title, :text) end end
###edit.html.erb
<h1>書籍を編集する</h1> <%= form_for :book, url: books_path(@book),method: :patch do |f| %> <p> <%= f.label :title %> <%= f.text_field :title %> </p> <p> <%= f.label :text %> <%= f.text_area :text %> </p> <p> <%= f.submit %> </p> <% end %> <%= link_to '戻る',books_path %>
###book.rb
class Book < ApplicationRecord validates :title, presence: true,length: {minimum: 5} end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/07 09:56