前提・実現したいこと
rails missing required keys: [:id]というエラーが解決できません。
当方rails初心者です。質問させて頂きます。
Showing /home/vagrant/work/bookers1-debug/app/views/books/index.html.erb where line #23 raised: No route matches {:action=>"show", :controller=>"books"}, missing required keys: [:id] <td><%= link_to 'Show', book_path(book)%></td> <td><%= link_to 'Edit', edit_book_path(book)%></td> <td><%= link_to 'Destroy', book_path, method: :delete, "data-confirm" => "Are you sure?" %></td> </tr> <% end %> コード
該当のソースコード
コントローラの記述
class BooksController < ApplicationController before_action :set_book, only: [:show, :edit, :update, :destroy] def index @book = Book.new @books = Book.all end def show @book = Book.find(params[:id]) end def edit @book = Book.find(params[:id]) redirect_to books_path end def create @book = Book.new(book_params) if @book.save flash[:notice] = 'Book was successfully created.' redirect_to book_path(@book) else render :index end 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 render :edit end end
book indexの記述
<% if flash[:notice] %> <div class="flash"><%= flash[:notice] %></div> <% end %> <h1>Books</h1> <table> <thead> <tr> <th>Title</th> <th>Body</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @books.each do |book| %> <tr> <td><%= book.title %></td> <td><%= book.body %></td> <td><%= link_to 'Show', book_path(book)%></td> <td><%= link_to 'Edit', edit_book_path(book)%></td> <td><%= link_to 'Destroy', book_path, method: :delete, "data-confirm" => "Are you sure?" %></td> </tr> <% end %> </tbody> </table> <h2>New book</h2> <%= render 'form', book: @book %> ```部分テンプレート部分
<% if flash[:notice] %>
<div class="flash"><%= flash[:notice] %></div> <% end %><%= 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 %>routse
1
Rails.application.routes.draw do
resources :books
end
book.rbの記述
class Book < ApplicationRecord
validates :title,presence: true
validates :body,presence: true
end
schema.rbの記述
ActiveRecord::Schema.define(version: 2020_05_15_114226) do
create_table "books", force: :cascade do |t|
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
end
end
ルート結果。
rails routes
Prefix Verb URI Pattern Controller#Action
books GET /books(.:format) books#index
POST /books(.:format) books#create
new_book GET /books/new(.:format) books#new
edit_book GET /books/:id/edit(.:format) books#edit
book GET /books/:id(.:format) books#show
PATCH /books/:id(.:format) books#update
PUT /books/:id(.:format) books#update
DELETE /books/:id(.:format) books#destroy
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
コード
### 試したこと No route matchesが出ていたため、コントローラのshowのrouteを確認したり,createのrenderなどを変更しましたが解決できず、ご質問させていただきます。
回答1件
あなたの回答
tips
プレビュー