UserテーブルとIntroductionテーブルをhas_oneで関連付けようとしました。
Console上ではうまくいくのにエラーが出るため、質問いたします。
今回 usersコントローラのshowビューにて
<p><%= @user.introduction.content %></p>
という埋め込みをしたところ
undefined method `content' for nil:NilClass
というエラーが出て、前に進みません。consoleなので確認しながら進めているのですが、わからないので知恵をご教授していただきたいです。
以下がmodelなどの構成要素となっています。
model
class User < ApplicationRecord mount_uploader :thumbnail, ThumbnailUploader has_many :microposts, dependent: :destroy has_one :introduction, dependent: :destroy class Introduction < ApplicationRecord belongs_to :user
migration
class CreateIntroductions < ActiveRecord::Migration[5.1] def change create_table :introductions do |t| t.text :content t.references :user, foreign_key: true t.timestamps end end end
rails console
>>user = User.first >>introduction = user.create_introduction(content: "a") >> user.introduction.content => "a"
あなたの回答
tips
プレビュー