No route matches {:action=>"show", :controller=>"books", :id=>nil}, missing required keys: [:id] とエラーが出てしまいます。
index
1<tbody> 2 <% @books.each do |book| %> 3 <tr> 4 <td><%= book.title %></td> 5 <td><%= book.body %></td> 6 <td><%= link_to 'Show', book_path(book), class: "show_#{@book.id}" %></td> 7 <td><%= link_to 'Edit', edit_book_path, class: "edit_#{@book.id}" %></td> ←エラー` 8 <td><%= link_to 'Destroy', book_path(book), method: :delete, data: { confirm: 'Are you sure?' }, class: "destroy_#{@book.id}" %></td> 9 </tr> 10 <% end %> 11 </tbody> 12</table> 13 14
controller
1 def show 2 @book = Book.find(params[:id]) 3 end 4 5 def create 6 @book = Book.new(book_params) 7 if @book.save 8 redirect_to book_path(@book), notice: "successfully created book!" 9 else 10 render 'index' 11 end 12 end 13 14 def edit 15 @book = Book.find(params[:id]) 16 end 17 18 def update 19 if @book.update() 20 redirect_to book_path(@book), notice: "successfully updated book!" 21 else 22 render 'edit' 23 end 24 end 25 26 def destroy 27 @book.destroy 28 redirect_to books_path, notice: "successfully delete book!" 29 end 30 31 private 32 33 def set_book 34 @book = Book.find(params[:id]) 35 end 36 37 def book_params 38 params.require(:book).permit(:title, :body) 39 end 40end 41
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。