現在、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
回答1件
あなたの回答
tips
プレビュー