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

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

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

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby on Rails

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

Q&A

解決済

1回答

1240閲覧

Railsを使用したアプリケーション開発でのエラーメッセージ NoMethodError

退会済みユーザー

退会済みユーザー

総合スコア0

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/11/20 03:37

前提・実現したいこと

本の感想を投稿するアプリケーションを開発中です。
Rails、deviseを使用しております。

投稿機能を実装中に以下のエラーメッセージが発生しました。
ご教授いただけると幸いです。

発生している問題・エラーメッセージ

######NoMethodError in BooksController
undefined method `user_id=' for #Book:0x00007f55203f9518 Did you mean? user=

8 def create 9 @book=Book.new(book_params) 10 @book.user_id = current_user.id 11 @book.save 12 redirect_to book_path(@book) 13 end

該当のソースコード

######books_controller

class BooksController < ApplicationController def index @user=current_user @books=Book.all @book=Book.new end def create @book=Book.new(book_params) @book.user_id = current_user.id @book.save redirect_to book_path(@book) end private def book_params params.require(:book).permit(:title,:body) end end

######views/books/index.html.erb

<div class="container "> <div class="row"> <div class="col-4"> <div class="wrapper-info mt-3"> <h2>User info</h2> <table class ="table" <th><%= @user.profile_image_id%></th> <tr> <td>name</td> <td><%= @user.name %></td> </tr> <tr> <td>introduction</td> <td><%= @user.introduction %></td> </tr> </table> <%= link_to "",user_path(current_user.id),class: "btn btn-outline-secondary btn-block fas fa-user-cog" %> </div> <div class="wrapper-newbook"> <h2>New book</h2> <%=form_with model:@book,local:true do |f| %> <p>Title</p> <%= f.text_field :title%> <p>Opinion</p> <%= f.text_area :body%> <%= f.submit "Create Book",class: "btn btn-success" %> <% end %> </div> </div> <div class="col-8"> <h2>Books</h2> <table class="table"> <th></th> <th>Title</th> <th>Opinion</th> <% @books.each do |book|%> <tr> <td> <%= attachment_image_tag book.user,:profile_image,:fill,60,60,fallback: "no_image.jpg"%> </td> <td> <%= books.title %> </td> <td> <%= books.body%> </td> </tr> <% end %> </table> </div> </div> </div>

######model/user.rb

class User < ApplicationRecord has_many :books,dependent: :destroy # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable attachment :profile_image end

######model/book.rb

class Book < ApplicationRecord belongs_to :user attachment :profile_image end

試したこと

エラー表記に従うと投稿ユーザーのidが取得できなくなってしまうので
他サイトにて方法を模索しましたが見当たりませんでした。

スペルチェックは実施済みです。

よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

マイグレーションファイルにuser_idを追記していなかったことが原因でした。

投稿2021/11/20 05:43

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問