前提・実現したいこと
railsで初めて開発しています
こちらの記事を参考に
ユーザーに紐づくlistを作成中にエラーが発生しました
発生している問題・エラーメッセージ
NoMethodError: undefined method `lists' for #<User::ActiveRecord_Relation:0x00005591611e0f80> /usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/relation/delegation.rb:125:in `method_missing' /lifehack/db/seeds.rb:8:in `<top (required)>' /usr/local/bundle/gems/activesupport-5.2.4.4/lib/active_support/dependencies.rb:285:in `load' /usr/local/bundle/gems/activesupport-5.2.4.4/lib/active_support/dependencies.rb:285:in `block in load' /usr/local/bundle/gems/activesupport-5.2.4.4/lib/active_support/dependencies.rb:257:in `load_dependency' /usr/local/bundle/gems/activesupport-5.2.4.4/lib/active_support/dependencies.rb:285:in `load' /usr/local/bundle/gems/railties-5.2.4.4/lib/rails/engine.rb:554:in `block in load_seed' /usr/local/bundle/gems/railties-5.2.4.4/lib/rails/engine.rb:672:in `with_inline_jobs' /usr/local/bundle/gems/railties-5.2.4.4/lib/rails/engine.rb:554:in `load_seed' /usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/tasks/database_tasks.rb:281:in `load_seed' /usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/railties/databases.rake:194:in `block (2 levels) in <top (required)>' /usr/local/bundle/gems/railties-5.2.4.4/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /usr/local/bundle/gems/railties-5.2.4.4/lib/rails/commands/rake/rake_command.rb:20:in `perform' /usr/local/bundle/gems/railties-5.2.4.4/lib/rails/command.rb:48:in `invoke' /usr/local/bundle/gems/railties-5.2.4.4/lib/rails/commands.rb:18:in `<top (required)>' /lifehack/bin/rails:9:in `require' /lifehack/bin/rails:9:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client.rb:30:in `run' /usr/local/bundle/gems/spring-2.1.1/bin/spring:49:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load' /usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>' /lifehack/bin/spring:15:in `require' /lifehack/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:seed (See full trace by running task with --trace)
該当のソースコード
db/seeds.rb
user = User.all user.lists.create!( [ { question: '健康:健やかで体調よく生きる' }, { question: '開放:新たな体験、発想、選択肢に心を開く' }, { question: '成長:変化と成長を維持する' }, { question: '知識:価値ある知識を学ぶ、または生み出す' }, { question: '美的:身のまわりの美しいものを味わう' }, { question: '快適:喜びに満ちた快適な人生を送る' }, { question: '合理:理性と論理に従う' }, { question: '目的:人生の意味と方向性を定める' }, { question: '寛容:自分と違う存在を尊重して受け入れる' }, { question: '自制:自分の行動を自分でコントロールする' }, { question: '孤独:他人から離れて1人でいられる時間と空間を持つ' }, { question: '真実:自分が正しいと思うとおりに行動する' }, { question: '現実:現実的、実践的にふるまう' }, { question: '体力:丈夫で強い身体を保つ' }, { question: '冒険:新しくてワクワクする体験をする' }, { question: '精神:精神的に成長し成熟する' }, { question: '現在:いまの瞬間に集中して生きる' }, { question: '笑い:人生や世界のユーモラスな側面を見る' },
app/models/list.rb
# == Schema Information # # Table name: lists # # id :bigint not null, primary key # answer :text(65535) # boss :text(65535) # disdain :text(65535) # done :boolean # event :text(65535) # guidance :text(65535) # question :text(65535) # rate :integer # respect :text(65535) # created_at :datetime not null # updated_at :datetime not null # user_id :bigint # # Indexes # # index_lists_on_user_id (user_id) # # Foreign Keys # # fk_rails_... (user_id => users.id) # class List < ApplicationRecord belongs_to :user validates :question, length: {maximum: 65535} validates :answer, length: {maximum: 65535} end
app/controllers/lists_controller.rb
class ListsController < ApplicationController before_action :logged_in_user, only: [:create, :destroy] before_action :correct_user, only: :destroy def index end def new @list = List.new end def create @list = current_user.lists.build(list_params) if @list.save flash[:success] = "成功しました!" redirect_to list_path(current_user) else render 'lists/new' end end def show @user = User.find(params[:id]) @lists = @user.lists end def update @list = List.find(params[:id]) if @list.update(list_params) flash[:success] = "編集しました!" redirect_to list_path(current_user) else render 'lists/edit' end end private def list_params params.require(:list).permit(:answer, :question) end def correct_user @list = current_user.lists.find_by(id: params[:id]) redirect_to root_url if @list.nil? end end
app/helpers/sessions_helper.rb
module SessionsHelper # 渡されたユーザーでログインする def log_in(user) session[:user_id] = user.id end # 記憶トークンcookieに対応するユーザーを返す def current_user if (user_id = session[:user_id]) @current_user ||= User.find_by(id: user_id) elsif (user_id = cookies.signed[:user_id]) user = User.find_by(id: user_id) if user && user.authenticated?(cookies[:remember_token]) log_in user @current_user = user end end end # 渡されたユーザーがログイン済みユーザーであればtrueを返す def current_user?(user) user == current_user end # ユーザーがログインしていればtrue、その他ならfalseを返す def logged_in? !current_user.nil? end # 永続的セッションを破棄する def forget(user) user.forget cookies.delete(:user_id) cookies.delete(:remember_token) end # 現在のユーザーをログアウトする def log_out forget(current_user) session.delete(:user_id) @current_user = nil end # 記憶したURL (もしくはデフォルト値) にリダイレクト(フレンドリーフォワーディング) def redirect_back_or(default) redirect_to(session[:forwarding_url] || default) session.delete(:forwarding_url) end # アクセスしようとしたURLを覚えておく def store_location session[:forwarding_url] = request.original_url if request.get? end end
app/models/user.rb
# == Schema Information # # Table name: users # # id :bigint not null, primary key # admin :boolean default(FALSE) # email :string(255) # name :string(255) # password_digest :string(255) # remember_digest :string(255) # created_at :datetime not null # updated_at :datetime not null # # Indexes # # index_users_on_email (email) UNIQUE # class User < ApplicationRecord has_many :lists, dependent: :destroy end
試したこと
記事からいろいろ試してみましたが、ダメでした・・・
環境
rails 5.2.3 ruby 2.5.7
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/20 11:15