質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

814閲覧

UrlGenerationErrorを解決できません。

k-keisuke

総合スコア3

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2020/04/29 05:38

前提・実現したいこと

rubyで、料理のレシピをカテゴリーごとに整理するアプリケーションを作成しております。
カテゴリー一覧から項目を選択するとその項目に関連するメニューの一覧へと遷移するようなプログラムを作成していたのですが、ActionController::UrlGenerationError in Menus#indexのようなエラーが発生してしまい、解決させることができないので、どなたか教えていただけないでしょうか。
エラー内容、関連コードは以下になります。

発生している問題・エラーメッセージ

ActionController::UrlGenerationError in Menus#index Showing /Users/kandakeisuke/individual/startcook/app/views/menus/_menu.html.haml where line #5 raised: No route matches {:action=>"show", :category_id=>#<ActiveRecord::Relation [#<Menu id: 2, title: "chige", detail: "ok", image: nil, created_at: "2020-04-28 04:02:40", updated_at: "2020-04-28 04:02:40", user_id: 1>, #<Menu id: 3, title: "changya", detail: "kkkkkkkkkkk", image: nil, created_at: "2020-04-28 06:31:18", updated_at: "2020-04-28 06:31:18", user_id: 1>]>, :controller=>"menus"}, missing required keys: [:id] Extracted source (around line #5): 3 4 5 6 7 8 .content__right .content__right__top = link_to menu.title, category_menu_path(@menus), method: :get, class: "content__right__top--title" .content__right__middle category: .content__right__bottom Trace of template inclusion: app/views/menus/index.html.haml Rails.root: /Users/kandakeisuke/individual/startcook

該当のソースコード

routes.rb

Rails.application.routes.draw do devise_for :users root "categories#index" resources :users, only: :show resources :categories, except: :index do resources :menus end end

categories_controller.rb

class CategoriesController < ApplicationController def index @categories = Category.all end def new @category = Category.new end def create Category.create(category_params) redirect_to root_path end def show @category = Category.find(params[:id]) @menus = @category render template: 'menus/index' end def edit @category = Category.find(params[:id]) end def update category = Category.find(params[:id]) category.update(category_params) redirect_to category_menus_path(category.id) end def destroy category = Category.find(params[:id]) menus = category.menus menus.destroy category.destroy redirect_to root_path end private def category_params params.require(:category).permit(:name).merge(user_id: current_user.id) end end

index.html.haml

- @categories.each do |category| = render category

_category.html.haml

.content .content__left .content__leftimage .content__right .content__right__top = link_to category.name, category_menus_path(category.id), class: "content__right__top--title" .content__right__bottom .content__right__bottom--userName = link_to category.user.nickname, user_path(category.user), class: "content__right__bottom--userName--btn"

menus_controller.rb

class MenusController < ApplicationController def index @menus = Menu.includes(:user) end def new @menu = Menu.new end def create Menu.create(menu_params) redirect_to category_menus_path end def show @menu = Menu.find(params[:id]) end def edit @menu = Menu.find(params[:id]) end def update menu = Menu.find(params[:id]) menu.update(menu_params) redirect_to category_menu_path(menu.id) end def destroy menu = Menu.find(params[:id]) menu.destroy redirect_to root_path end private def menu_params params.require(:menu).permit(:title, :detail).merge(user_id: current_user.id, category_id: params[:category_id]) end end

index.html.haml

.content .content__top -# = link_to @menus.category.name, edit_category_path(@menus.category.id), class: "menuManage__edit" - @menus.each do |menu| = render menu

_menu.html.haml

.content__left .content__leftimage .content__right .content__right__top = link_to menu.title, category_menu_path(@menus), method: :get, class: "content__right__top--title" .content__right__middle category: .content__right__bottom .content__right__bottom--userName = link_to menu.user.nickname, user_path(menu.user), class: "content__right__bottom--userName--btn" .content__right__bottom--date = menu.created_at

試したこと

categoryコントローラーから該当のidが送信できていないと考え、edit_category_path(@menus.category.id)の引数のように、いろいろな形で変数の代入を試みましたが、解決には至れませんでした。
どなたかわかる方、ご教示お願い申し上げます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

多分

index.html.haml

haml

1 -# = link_to @menus.category.name, edit_category_path, class: "menuManage__edit"

_menu.html.haml

haml

1 = link_to menu.title, category_menu_path(menu), method: :get, class: "content__right__top--title"

投稿2020/04/29 07:09

asm

総合スコア15147

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

k-keisuke

2020/04/30 03:30

大変助かりました! ご教示ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問