前提・実現したいこと
料理のレシピを整理するアプリケーション作成しています。
トップの一覧表にランダムボタンを設置し、2階層下の個別のメニュー詳細画面に遷移するようなリンクを実装したいと考えています。
しかし、ボタンを押下すると以下のような内容のエラーが発生しています。
もしわかられる方がおられましたら、ご教示お願いいたします。
発生している問題・エラーメッセージ
NoMethodError in CategoriesController#random undefined method `category_id' for #<Array:0x00007f9cc2a5a030> Extracted source (around line #41): 39 40 41 42 43 44 def random menu = Menu.all.shuffle.first(1) redirect_to url: category_menu_path(menu.category_id, menu.id) # Category.random end Rails.root: /Users/kandakeisuke/individual/st_cook Application Trace | Framework Trace | Full Trace app/controllers/categories_controller.rb:41:in `random'
該当のソースコード
categories/index.html.haml
.rd = link_to "today's cook", random_categories_path = form_with(url: categories_searches_path, local: true, method: :get, class: "search-form") do |f| = f.text_field :keyword, placeholder: "カテゴリーを検索する", class: "search-input", id: "keyword" = f.submit "検索", class: "search-btn" - @categories.each do |category| = render category = paginate(@categories)
categories_controller.rb
def show @menus = @category.menus.includes(:user) # @menu = Menu.find(params[:id]) end def destroy category = Category.find(params[:id]) category.menus.destroy category.destroy redirect_to root_path end def random menu = Menu.all.shuffle.first(1) redirect_to url: category_menu_path(menu.category_id, menu.id) # Category.random end private def category_params params.require(:category).permit(:name).merge(user_id: current_user.id) end def set_category @category = Category.find(params[:id]) end def move_to_index redirect_to action: :index unless user_signed_in? end end
menus/index.html.haml
.menuTitle = @menu.title .menuData = @menu.created_at .menuAuthor - if user_signed_in? && current_user.id == @menu.user_id .menuManage = link_to "編集", edit_category_menu_path(@menu.category_id, @menu.id), class: "menuManage__edit" = link_to "削除", category_menu_path(@menu.category_id, @menu.id), method: :delete, class: "menuManage__delete" .menuText = image_tag "#{@menu.image}", class: :imop if @menu.image.length > 0 = simple_format @menu.detail
$ rails routes
Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create root GET / categories#index categories_searches GET /categories/searches(.:format) categories/searches#index category_menus_searches GET /categories/:category_id/menus/searches(.:format) menus/searches#index category_menus GET /categories/:category_id/menus(.:format) menus#index POST /categories/:category_id/menus(.:format) menus#create new_category_menu GET /categories/:category_id/menus/new(.:format) menus#new edit_category_menu GET /categories/:category_id/menus/:id/edit(.:format) menus#edit category_menu GET /categories/:category_id/menus/:id(.:format) menus#show PATCH /categories/:category_id/menus/:id(.:format) menus#update PUT /categories/:category_id/menus/:id(.:format) menus#update DELETE /categories/:category_id/menus/:id(.:format) menus#destroy random_categories GET /categories/random(.:format) categories#random categories POST /categories(.:format) categories#create new_category GET /categories/new(.:format) categories#new edit_category GET /categories/:id/edit(.:format) categories#edit category GET /categories/:id(.:format) categories#show PATCH /categories/:id(.:format) categories#update PUT /categories/:id(.:format) categories#update DELETE /categories/:id(.:format) categories#destroy user GET /users/:id(.:format) users#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
試したこと
randomアクションについて
・変数menuを@menuでやってみる
・category_menu_path(menu.category_id, menu.id)の二つの引数を逆にしてみる
・Menu.all.shuffle.first(1)をMenu.all.shuffle.first(1).idとしてみる
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/07 09:29
2020/05/07 10:11
2020/05/08 04:37
2020/05/08 07:04
2020/05/09 10:22
2020/05/28 09:47