質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

1604閲覧

NameError in BooksController#destroyのエラー

yukitodesu3

総合スコア10

Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2021/02/25 15:47

編集2021/02/27 06:25

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

試したこと
・エラー内容の確認
・コードの確認

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shinoharat2

2021/02/27 00:05

【お願い1】 app/models/post_comment.rb というファイルが存在しますか? 存在するなら、そのファイルの中身を見せてください。 【お願い2】 bookモデルのコードはこれで全部ですか? 省略した部分はありませんか?
yukitodesu3

2021/02/27 06:21

コメントありがとうございます。 修正します。 bookモデルのコードはこれで全部です。
yukitodesu3

2021/02/27 06:24

user.rbならあります。
shinoharat2

2021/02/27 10:45

あれ? app/models/post_comment.rb というファイルは無いんですか? Book クラスに 「has_many :post_comment」 と定義されていたので、PostComment というモデルがあるのかと思っていたのですが・・・ そうすると、 【Q1】 なぜ Book に「has_many :post_comment」と定義してあるのか? 【Q2】 PostComment クラスはどこに定義していあるのか? (あるいはどこにもないのか) の2点をお伺いしたいです。
yukitodesu3

2021/02/27 10:58

コメントありがとうございます。 post_commentのモデルは間違えて記述してしまったものでした。紛らわしいことをしてしまい、すみません。 destroyの問題については解決いたしました。 たびたびのコメントありがとうございました。
guest

回答2

0

Bookモデル内に下記のような記述はしていませんか??

rb

1class Book < ActiveRecord::Base 2 // この下の一行 3 belongs_to user, :dependent => :destroy 4end

この場合、:dependent => :destroy削除することで解決する可能性があります。

注意点:
削除を連動させたいなら、Userモデル側で:dependent => :destroy必要です。

rb

1class User < ActiveRecord::Base 2 // この下の一行 3 has_many books, :dependent => :destroy 4end

:dependent => :destroyについて
他の可能性

投稿2021/02/25 16:47

yuyakanda

総合スコア17

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yukitodesu3

2021/02/26 13:16

:dependent => :destroyを排除してみたのですが、エラーは解決されませんでした。 bookモデルのコードも貼っておきます。
guest

0

自己解決

imageのモデルを間違えて作ってしまい、エラーが発生しました。

rails generate migration RemoveImageUsers author:string
とターミナルに記述したところ、エラーが解消されました。

投稿2021/02/27 11:02

yukitodesu3

総合スコア10

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問