RailsでCRUD処理を実行できる簡単なアプリケーションを作って練習中です。
書籍登録のアプリです。
indexの画面に表示、編集、削除のリンクが表示されません。
つい先ほどまでは表示されていて、機能もしていました。
その間の作業は、画面表示の並びがかっこ悪かったので、indexのHTMLをいじっていました。
それとすでに保存された書籍を削除しました。(みやすくしたかったので一旦消した)
なぜかわからなかったのでサーバーを解いて、再び開くなどしたり試しました。
どこか誤まって触れてしまったかもしれませんが、検討がつかないので、一緒にみてもらえないでしょうか。
以下が編集した内容です。よろしくお願いいたします。
:追記
edit.html.erbの<%= form_for(@book) do |f| %>
は、<%= form_for :book, url: books_path(@book),method: :patch do |f| %>
から変更したものです。ここに原因がありますでしょうか
###index.html.erb
<h1>書籍一覧</h1> <%= link_to '新しい書籍を登録',new_book_path %> <table> <tr> <th>タイトル</th> <th>テキスト</th> <th colspan="3"></th> </tr> <% @books.each do |book| %> <tr> <td><%= book.title %></td> <td><%= book.text %></td> <td><%= link_to '表示',book_path(book) %></td> <td><%= link_to '編集',edit_book_path(book) %></td> <td><%= link_to '削除',book_path(book), method: :delete, data: {confirm: '本当に削除してもいいですか?'} %> </td> </tr> <% end %> </table>
###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
###new.html.erb
<h1>新しい書籍</h1> <%= form_for :book, url: books_path 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 %>
###edit.html.erb
<h1>書籍を編集する</h1> <%= form_for(@book) 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
回答2件
あなたの回答
tips
プレビュー