前提・実現したいこと
missing required keys: [:id]エラーを解消してcard_listのshowページを開く
発生している問題・エラーメッセージ
エラーメッセージ ActionController::UrlGenerationError (No route matches {:action=>"show", :admin_id=>#<CardList id: 1, admin_id: 1, name: "久兵衛", address: "東京", opening_hours: "2000-01-01 12:00:00", closing_hours: "2000-01-01 22:00:00", phone_num: "9999999999", url: "http://www.kyubey.jp/", created_at: "2020-03-20 08:22:26", updated_at: "2020-03-20 08:22:26">, :controller=>"card_lists"}, missing required keys: [:id]):
該当のソースコード
card_lists_controller.rb class CardListsController < ApplicationController def new @card_list = CardList.new end def create @card_list = CardList.new(card_list_params) if @card_list.save redirect_to admin_card_list_url(@card_list), notice: "カード登録が完了しました" else render "new" end end def show @card_list = CardList.find(params[:id]) @point_lists = PointList.where(card_list_id: params[:card_list_id]) @coupon_lists = CouponList.where(card_list_id: params[:card_list_id]) 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
createアクションのredirect_to admin_card_list_url(@card_list)の部分でエラーが起きています。
疑問
パラメータを見るとcard_listのidが入っているように見えるのですが、なぜidがないと言われるかがわかりません。
パラメータが:admin_id=>#となっているのが問題でしょうか?
ご教授のほどよろしくお願いいたします。
補足情報(FW/ツールのバージョンなど)
admins_controller.rb class AdminsController < ApplicationController include Admins::SessionsHelper def new @admin = Admin.new end def create @admin = Admin.new(admin_params) if @admin.save log_in @admin redirect_to new_admin_card_list_url(@admin), notice:"アカウント登録が完了しました" else render "new" end end private def admin_params params.require(:admin).permit(:name, :email, :admin, :password, :password_confirmation) end end
上記のcreateが完了したらcard_listのnewページに飛びます
views/cards/new.html.haml =render "layouts/header" .card-list-new-contents .new-content %h2 店舗情報を入力してください =form_with model:@card_list, url: admin_card_lists_url, method: :post, class: "admin-card__form" do |f| .admin-card__form__relation__input = f.label :name, "店舗名", class: "label" = f.text_field :name, class: "form-input name" .admin-card__form__relation__input = f.label :address, "住所", class: "label" = f.text_field :address, class: "form-input__address" .admin-card__form__relation__input = f.label :opening_hours, "開店時刻", class: "label" = f.time_field :opening_hours, prompt: "--", type: "time", class: "form-input__hours" .admin-card__form__relation__input = f.label :closing_hours, "閉店時刻", class: "label" = f.time_field :closing_hours, prompt: "--", type: "time", class: "form-input__hours" .admin-card__form__relation__input = f.label :url, "URL", class: "label" = f.text_field :url, class: "form-input__url" .admin-card__form__relation__input = f.label :phone_num, "電話番号", class: "label" = f.text_field :phone_num, class: "form-input phone__num" .admin-card__form__relation__input = f.label :image, "アイコン画像", class: "label" = f.file_field :image, class: "form-input__image" .admin-card__form__submit = f.submit "店舗情報を登録する", class: 'btn btn-primary'
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 user_cards GET /users/:user_id/cards(.:format) cards#index POST /users/:user_id/cards(.:format) cards#create new_user_card GET /users/:user_id/cards/new(.:format) cards#new 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_lists 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 admin_card_list GET /admins/:admin_id/card_lists/:id(.:format) card_lists#show admins GET /admins(.:format) admins#index POST /admins(.:format) admins#create new_admin 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 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 GET /card_lists(.:format) card_lists#index 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
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/20 09:03