raillsでアプリを作成中NameError in BooksController#destroyのエラーが出ました。何が原因かわからず困っています。
エラーの内容を確認し、コードをみてみたのですが、どこがどう間違っているのかわかりませでした。お手数ですが、回答よろしくお願いします。
book.html.contlloer
class BooksController < ApplicationController def index @books = Book.all @user = current_user end def show @book = Book.find(params[:id]) @user = current_user end def new end def create @user = current_user @books = Book.all @book = Book.new(book_params) @book.user_id = current_user.id @book.save if @book.save redirect_to book_path(@book.id) else redirect_to user_path(@user.id) end end def edit @book = Book.find(params[:id]) end def update @book = Book.find(params[:id]) @book.update(book_params) redirect_to book_path(@book.id) end def destroy @book = Book.find(params[:id]) @book.destroy redirect_to "books" end private def book_params params.require(:book).permit(:title, :body) end end
routers.rb
Rails.application.routes.draw do resources :books, only: [:new, :create, :index, :show, :destroy] devise_for :users, controllers: { sessions: 'users/sessions', registrations: 'users/registrations', } # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root 'homes#top' get 'books' => 'books#index' post 'books' => 'books#create' get 'books/:id/edit' => 'books#edit', as: 'edit_book' patch 'books/:id' => 'books#update', as: 'update_book' resources :users, only: [:show, :edit, :update] end
show.html.erb
<main> <% if @book.errors.any? %> <%= @book.errors.count %>errors prohibited this obj from being saved: <% @book.errors.full_messages.each do |message| %> <%= message %> <% end %> <% end %> <div class="book-user"> <div class="book-user-title"> <h1>User info</h1> </div> <div class="profile-container"> <h3 class="profile-name"> <h3>name</h3> <%= @user.name %> </h3> <%= attachment_image_tag @user, :profile_image, :fill, 100, 100, format: 'jpeg', fallback: "no_image.jpg" %> <% if @user.id == current_user.id %> <p><%= link_to "プロフィール編集", edit_user_path(@user) %></p> <% end %> <h3 class="profile-introduction"> <h3>introduction</h3> <%= @user.introduction %> </h3> </div> </div> <%= form_with model:@book, local:true do |f| %> <div class="book-title"> <h1>New book</h1> <div class="book-list"> <div class="book-item"> <h4>Title</h4> <%= f.text_field :title %> </div> <div class="book-item2"> <h4>Opinion</h4> <%= f.text_area :body %> </div> </div> <div class="book-submit"> <%= f.submit 'Create book' %> </div> </div> <% end %> <div class="right-book"> <div class="right-book-title"> <h1>Book detail</h1> </div> <div class="right-books-img"> <%= attachment_image_tag @user, :profile_image, :fill, 100, 100, format: 'jpeg', fallback: "no_image.jpg" %> </div> <div class="right-books-list"> <%= @book.title %> <%= @book.body %> </div> <div class="right-books-item"> <%=link_to 'Edit',edit_book_path(@book.id) %> <%= link_to "削除", book_path(@book.id), method: :delete, "data-confirm" => "本当に消しますか?" %> </div> </div> </main>
book.rb
class Book < ApplicationRecord belongs_to :user attachment :image has_many :post_comments validates :title, presence: true validates :body, presence: true, length: { maximum: 200 } end
user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :books, dependent: :destroy attachment :profile_image validates :name, uniqueness:true, presence: true, length: { minimum: 2, maximum: 20 } validates :introduction, length: {maximum: 50 } end
試したこと
・エラー内容の確認
・コードの確認
【お願い1】
app/models/post_comment.rb というファイルが存在しますか?
存在するなら、そのファイルの中身を見せてください。
【お願い2】
bookモデルのコードはこれで全部ですか?
省略した部分はありませんか?
コメントありがとうございます。
修正します。
bookモデルのコードはこれで全部です。
user.rbならあります。
あれ? app/models/post_comment.rb というファイルは無いんですか?
Book クラスに
「has_many :post_comment」
と定義されていたので、PostComment というモデルがあるのかと思っていたのですが・・・
そうすると、
【Q1】
なぜ Book に「has_many :post_comment」と定義してあるのか?
【Q2】
PostComment クラスはどこに定義していあるのか?
(あるいはどこにもないのか)
の2点をお伺いしたいです。
コメントありがとうございます。
post_commentのモデルは間違えて記述してしまったものでした。紛らわしいことをしてしまい、すみません。
destroyの問題については解決いたしました。
たびたびのコメントありがとうございました。
回答2件
あなたの回答
tips
プレビュー