前提・実現したいこと
初めて質問させていただきます。
railsでポイント管理サービスをつくっています。
ポイント特典を作成する機能を実装中にエラーが発生しました。
親要素(card_list)のindexページから子要素(point_list)のnewページに飛ばすpathでエラーが発生しています。
エラー文から親要素のidが取得できれば解決できると考えています。
なので、親要素(card_list)のidをpoint_listsコントローラーで子要素(point_list)へ渡せるようにしたいです。
発生している問題・エラーメッセージ
point_listの親要素であるcard_listのidが空であると言われています。
エラーメッセージ
No route matches {:action=>"new", :admin_id=>#<CardList id: 2, admin_id: 2, name: "カレー", address: "大阪", opening_hours: "2000-01-01 11:30:00", closing_hours: "2000-01-01 23:00:00", phone_num: "0000000000", url: "https://www.ashoka-jp.com/", created_at: "2020-03-14 06:52:09", updated_at: "2020-03-14 06:52:09">, :controller=>"point_lists"}, missing required keys: [:card_list_id]
### 該当のソースコード =link_to "追加", new_admin_card_list_point_list_path(@card_list), class: "index-contents__middle__points__add btn btn-success"
試したこと
point_listsコントローラーのnewアクションにcard_list_idが得られるような記述を追加しました
補足情報(FW/ツールのバージョンなど)
card_lists_conroller.rb class CardListsController < ApplicationController def index @card_list = CardList.find(params[:admin_id]) end def new @card_list = CardList.new end def create @card_list = CardList.new(card_list_params) if @card_list.save redirect_to admin_card_lists_path(@card_list), notice: "カード登録が完了しました" else render "new" end end private def card_list_params params.require(:card_list).permit( :name, :address, :opening_hours, :closing_hours, :phone_num, :url, :image).merge(admin_id: session[:admin_id]) end end
point_lists_controller.rb class PointListsController < ApplicationController def new @card_list = CardList.find(params[:card_list_id]) @point_list = PointList.new(card_list_id: @card_list_id) end def create @point_list = PointList.new(point_list_params) if @point_list.save redirect_to card_lists_parh, notice: "特典を追加しました" else render "new" end end private def point_list_params params.require(:point_list).permit(:content).merge(card_list_id: session[:card_list_id]) end end
rails routesの実行結果がこちらになります
Prefix Verb URI Pattern Controller#Action root GET / introduction#home 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 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 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 admin_card_list_point_lists POST /admins/:admin_id/card_lists/:card_list_id/point_lists(.:format) point_lists#create new_admin_card_list_point_list GET /admins/:admin_id/card_lists/:card_list_id/point_lists/new(.:format) point_lists#new admin_card_list_coupon_lists POST /admins/:admin_id/card_lists/:card_list_id/coupon_lists(.:format) coupon_lists#create new_admin_card_list_coupon_list GET /admins/:admin_id/card_lists/:card_list_id/coupon_lists/new(.:format) coupon_lists#new admin_card_lists GET /admins/:admin_id/card_lists(.:format) card_lists#index POST /admins/:admin_id/card_lists(.:format) card_lists#create new_admin_card_list GET /admins/:admin_id/card_lists/new(.:format) card_lists#new admins POST /admins(.:format) admins#create new_admin GET /admins/new(.:format) admins#new GET /admins(.:format) admins#index POST /admins(.:format) admins#create GET /admins/new(.:format) admins#new edit_admin GET /admins/:id/edit(.:format) admins#edit admin GET /admins/:id(.:format) admins#show PATCH /admins/:id(.:format) admins#update PUT /admins/:id(.:format) admins#update DELETE /admins/:id(.:format) admins#destroy 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
routes.rb Rails.application.routes.draw do root "introduction#home" # 消費者ユーザー namespace :users, only: [:new, :create] do get "/login", to: "sessions#new" post "/login", to: "sessions#create" delete "/logout", to: "sessions#destroy" end resources :users # 管理者ユーザー namespace :admins do get "/login", to: "sessions#new" post "/login", to: "sessions#create" delete "/logout", to: "sessions#destroy" end resources :admins, only: [:new, :create] do resources :card_lists, only: [:index, :new, :create] do resources :point_lists, only: [:new, :create] resources :coupon_lists, only: [:new, :create] end end resources :admins end
views/card_lists/index.html.haml =render "layouts/header" .card_list-index-contents .index-contents .index-contents__upper .index-contents__upper__image = image_tag @card_list.image, size: "166x150" %h2.index-contents__upper__name =@card_list.name %ul.index-contents__upper__info %li.index-contents__upper__address ="住所: #{@card_list.address}" %li.index-contents__upper__openig ="営業時間: #{@card_list.opening_hours.strftime('%H:%M')} 〜 #{@card_list.closing_hours.strftime('%H:%M')}" %li.index-contents__upper_tel ="TEL: #{@card_list.phone_num}" %li.index-contents__upper__url ="URL:" =auto_link(@card_list.url, html: { target: '_blank' }) .index-contents__upper__btn =link_to "編集", "#", class: "index-contents__upper__btn__edit btn btn-warning" =link_to "削除", "#", class: "index-contents__upper__btn__delete btn btn-danger" .index-contents__middle %h2.index-contents__middle__points ポイント特典 =link_to "追加", "#", class: "index-contents__middle__points__add btn btn-success" %h2.index-contents__middle__coupons クーポン =link_to "追加", "#", class: "index-contents__middle__coupons__add btn btn-success"
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。