前提・実現したいこと
railsでwebアプリケーションを制作しています。
モデルの同士の関連付けたいです
card_list(親)とpoint_list(子)のモデルです
発生している問題・エラーメッセージ
NoMethodError in PointListsController#create undefined method `point_lists' for nil:NilClass
該当のソースコード
point_lists_controller.rb class PointListsController < ApplicationController def new @point_list = PointList.new end def create @point_list = @card_list.point_lists.new(point_list_params) if @point_list.save redirect_to card_list_url(@card_list), notice: "特典を追加しました" else render "new" end end private def point_list_params params.require(:point_list).permit(:content) end end
試したこと
モデルに一対多の関係を記述しました
cardlist:一, point_list:多の関係です
ご教授のほどよろしくお願いいたします
補足情報(FW/ツールのバージョンなど)
card_list.rb class CardList < ApplicationRecord belongs_to :admin has_one_attached :image has_one :card has_many :point_lists has_many :coupon_lists end
point_list.rb class PointList < ApplicationRecord belongs_to :card_list end
point_listのマイグレーションファイル class CreatePointLists < ActiveRecord::Migration[5.2] def change create_table :point_lists do |t| t.references :card_list, null: false, index: true t.text :content, null: false t.timestamps end end end
card_lists/show.html.hamlの一部 %h2.index-contents__middle__plus ポイント特典 %br -if @point_lists.present? - @point_lists.each do |point_list| .index-contents__middle__plus__info .index-contents__middle__plus__info__description =point_list.content .index-contents__middle__plus__info__btn =link_to "編集", "#", class: "index-contents__middle__plus__info__btn__edit btn btn-warning" =link_to "削除", "#", class: "index-contents__middle__plus__info__btn__delete btn btn-danger" %br =link_to "追加", new_card_list_point_list_path(@card_list), class: "index-contents__middle__plus__add btn btn-success"
一番下の追加ボタンを押すとpoint_listのnewアクションが動作します。
rails routesの結果です Prefix Verb URI Pattern Controller#Action root GET / introduction#home users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy users_login GET /users/login(.:format) users/sessions#new POST /users/login(.:format) users/sessions#create users_logout DELETE /users/logout(.:format) users/sessions#destroy cards GET /cards(.:format) cards#index POST /cards(.:format) cards#create new_card GET /cards/new(.:format) cards#new admins_login GET /admins/login(.:format) admins/sessions#new POST /admins/login(.:format) admins/sessions#create admins_logout DELETE /admins/logout(.:format) admins/sessions#destroy admins POST /admins(.:format) admins#create new_admin GET /admins/new(.:format) admins#new card_list_point_lists POST /card_lists/:card_list_id/point_lists(.:format) point_lists#create new_card_list_point_list GET /card_lists/:card_list_id/point_lists/new(.:format) point_lists#new card_list_coupon_lists POST /card_lists/:card_list_id/coupon_lists(.:format) coupon_lists#create new_card_list_coupon_list GET /card_lists/:card_list_id/coupon_lists/new(.:format) coupon_lists#new card_lists POST /card_lists(.:format) card_lists#create new_card_list GET /card_lists/new(.:format) card_lists#new card_list GET /card_lists/:id(.:format) card_lists#show rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
ここにより詳細な情報を記載してください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/21 09:03
2020/03/21 09:39
2020/03/21 10:13
2020/03/21 13:32