前提・実現したいこと
データベースから1つのレコードを取得して、そのレコードの特定のフィールドの値をビューに出力したいと思っています。
例えばexamというテーブルからquestionというフィールドの値をビューに出力したいです。
発生している問題・エラーメッセージ
undefined method `question' for #<Array:0x00007ff3c4255958>
該当のソースコード
Ruby
1#controller 2class ExamsController < ApplicationController 3 def question 4 @exam = Exam.all.sample(1) 5 end 6end 7 8#view 9<p class="center">問. <%= @exam.question %></p>
Ruby
1#migrate 2class CreateExams < ActiveRecord::Migration[5.2] 3 def change 4 create_table :exams do |t| 5 t.text :question 6 t.text :answer 7 8 t.timestamps 9 end 10 end 11end
Ruby
1#model 2class Exam < ApplicationRecord 3end
試したこと
フィールド名が違うと思い、migrateファイルのフィールド名をコピペしましたが駄目でした...
他ググりましたが、適当な回答が見つからず...
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。