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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Q&A

解決済

1回答

772閲覧

ruby on rails で削除機能をつけたいです。

nijima

総合スコア27

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/08/13 11:05

現在、ruby on rails 本のタイトルと本の内容を投稿するアプリをつくっているのですが、新規投稿した本を削除しようとすると、「undefined method `image_id' for #Book:0x00007fdf0c1d42f8 Did you mean? image」
というエラーがでます。しかし、image_idを登録した覚えがなく混乱しています。
ご指導お願いいたします。イメージ説明

class BooksController < ApplicationController def create @books = Book.all @book = Book.new(book_params) @book.user_id = current_user.id @book.save redirect_to books_path end def index @newbook = Book.new @books = Book.all @user = current_user end def show @newbook = Book.new @book = Book.find(params[:id]) @user = current_user end def destroy @book = Book.find(params[:id]) @book.destroy redirect_to books_path end private def book_params params.require(:book).permit(:title, :body) end end
<h2>User info</h2> <%= render 'users/user', user: @user %> <h1>New Book</h1> <%= render 'books/book', book: @book %> <h2>Book detail</h2> <%= @book.title %> <%= @book.body %> <% if @book.user == current_user %> <%= link_to "Destroy", book_path(@book), method: :delete %> <% end %>
# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2021_08_13_075141) do create_table "books", force: :cascade do |t| t.text "title" t.text "body" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "introduction" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end

rails routes

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

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

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

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

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

kokitail

2021/08/13 12:21

bookモデルにアソシエーション組んでたりしませんか?
nijima

2021/08/13 12:33

bookモデルに画像のアソシエーションを組んでいました。 このアソシエーションを削除したところ、削除エラーを解決できました!! 誠にありがとうございました!
kokitail

2021/08/13 12:38

アソシエーションの削除が本当に正しい解決方法でしょうか? アソシエーションは必要があって設定してたものではないですか?
nijima

2021/08/13 12:40

はい、今回のbookモデルでは画像投稿機能をつけてないので大丈夫です。おそらく、userモデルにかくつもりが、誤ってbookモデルにかきました。
guest

回答1

0

自己解決

bookモデルのimageのアソシエーションを消すと解決しました。

投稿2021/08/13 12:34

nijima

総合スコア27

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問