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

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

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

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

Ruby on Rails 6

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

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

Q&A

解決済

1回答

1082閲覧

[Heroku]本番環境でのエラーについて

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby

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

Ruby on Rails 6

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

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

0グッド

0クリップ

投稿2021/03/22 08:26

編集2021/03/24 00:49

現状

フロントエンド %ul.sub %li = link_to "①HTML", card_category_path(:card_id ,1) %li = link_to "②CSS", card_category_path(:card_id, 2) %li = link_to "③JavaScript", card_category_path(:card_id, 3) %li バックエンド %ul.sub %li = link_to "④Java", card_category_path(:card_id, 4) %li = link_to "⑤PHP", card_category_path(:card_id, 5) %li = link_to "⑥Python", card_category_path(:card_id, 6) %li = link_to "⑦Ruby", card_category_path(:card_id, 7)

上記のコードの④だけクリックするとエラーは起きなくて、それ以外は
ActiveRecord::RecordNotFound in CategoriesController#show
Couldn't find Category with 'id'=1
というエラーが起きてしまう。
しかし④はエラーは起きないもののHTMLの登録一覧と表示されてしまう。。。本来はJavaの登録一覧と表示されるのが理想です

関連ありそうなコード

カテゴリーコントローラーになります。

class CategoriesController < ApplicationController def show @category = Category.find(params[:id]) @cards = Card.where(category_id: params[:id]).page(params[:page]).per(7) end end

表示するビューになります。

.Table .Table__title = "「#{@category.name}」の登録一覧" .Table__lists .Table__lists__list - @cards.each do |card| .Table__lists__list__title = link_to "#{card.title}", card_path(card.id) .Table__lists__list__btn = link_to "編集", edit_card_path(card.id) .Table__lists__list__btn = link_to "削除", card_path(card.id), method: :delete .Pagenation = paginate @cards .Return .Return__btn = link_to "トップページに戻る", root_path

次はデータを入れているseeds.rbになります

Category.create(:name => 'HTML', :card_id => 1) Category.create(:name => 'CSS', :card_id => 2) Category.create(:name => 'JavaScript', :card_id => 3) Category.create(:name => 'Java', :card_id => 4) Category.create(:name => 'PHP', :card_id => 5) Category.create(:name => 'Python', :card_id => 6) Category.create(:name => 'Ruby', :card_id => 7)

試したこと

ターミナルで

heroku run RAILS_ENV=production rails db:seed

を実行しエラー起きずに成功

Running RAILS_ENV=production rails db:seed on ⬢ engineerbook-keitaro... up, run.1500 (Free) D, [2021-03-19T01:45:54.039767 #4] DEBUG -- : (9.3ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483 D, [2021-03-19T01:45:54.097473 #4] DEBUG -- : (6.8ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC D, [2021-03-19T01:45:54.232651 #4] DEBUG -- : (2.7ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483 D, [2021-03-19T01:45:54.321694 #4] DEBUG -- : (2.7ms) BEGIN D, [2021-03-19T01:45:54.327589 #4] DEBUG -- : Category Create (5.6ms) INSERT INTO `categories` (`name`, `card_id`, `created_at`, `updated_at`) VALUES ('HTML', 1, '2021-03-19 01:45:54', '2021-03-19 01:45:54') D, [2021-03-19T01:45:54.344828 #4] DEBUG -- : (16.6ms) COMMIT D, [2021-03-19T01:45:54.357923 #4] DEBUG -- : (11.0ms) BEGIN D, [2021-03-19T01:45:54.368853 #4] DEBUG -- : Category Create (10.7ms) INSERT INTO `categories` (`name`, `card_id`, `created_at`, `updated_at`) VALUES ('CSS', 2, '2021-03-19 01:45:54', '2021-03-19 01:45:54') D, [2021-03-19T01:45:54.395439 #4] DEBUG -- : (25.9ms) COMMIT D, [2021-03-19T01:45:54.411340 #4] DEBUG -- : (11.6ms) BEGIN D, [2021-03-19T01:45:54.415574 #4] DEBUG -- : Category Create (3.1ms) INSERT INTO `categories` (`name`, `card_id`, `created_at`, `updated_at`) VALUES ('JavaScript', 3, 省略

どなたかお助けしてくれると嬉しいです。宜しくお願い致します。

追加

ルーティングになります

Rails.application.routes.draw do devise_for :users root to: 'cards#index' #トップページへ遷移 resources :cards do#7つのアクションの省略 collection do get 'search' end resources :categories, only: [:show] end end

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 / cards#index search_cards GET /cards/search(.:format) cards#search card_category GET /cards/:card_id/categories/:id(.:format) categories#show cards GET /cards(.:format) cards#index POST /cards(.:format) cards#create new_card GET /cards/new(.:format) cards#new edit_card GET /cards/:id/edit(.:format) cards#edit card GET /cards/:id(.:format) cards#show PATCH /cards/:id(.:format) cards#update PUT /cards/:id(.:format) cards#update DELETE /cards/:id(.:format) cards#destroy

cardモデルになります

class Card < ApplicationRecord belongs_to :category, optional: true def self.search(search) #self.クラスメソッド if search != "" Card.where('language LIKE(?) or title LIKE(?)', "%#{search}%","%#{search}%").order('id DESC') #言語、タイトルで検索することが可能 else order('Id DESC') end end end

カテゴリーモデルになります

class Category < ApplicationRecord has_many :cards end

画像の追加

イメージ説明

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

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

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

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

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

winterboum

2021/03/22 10:55

`card_category_path(:card_id ,1)` の書き方に違和感。 routes.rb と rails routes 見せてください。 あと どうも全体が見えない。モデルのcodeも載せてください
退会済みユーザー

退会済みユーザー

2021/03/22 12:24

何度も手助け本当に感謝しております。 指摘していただいた箇所追加しました。 よろしくお願いします。
guest

回答1

0

ベストアンサー

card_category_path(:card_id ,1) の書き方に違和感。
card_category GET /cards/:card_id/categories/:id ですから
card_category_path(1, @category.id)
なのですが、、、、、
まだなんか違和感。

投稿2021/03/22 12:37

winterboum

総合スコア23349

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

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

退会済みユーザー

退会済みユーザー

2021/03/22 12:43

指摘していただいたようにしてみたのですが、 NoMethodError in Cards#index エラーがやはり出てしまいます。。。
winterboum

2021/03/22 22:47

そういうとき、エラーがでたではなくエラーメッセージ全体を載せてください
退会済みユーザー

退会済みユーザー

2021/03/23 00:52 編集

かしこまりました。 Showing /Users/inouekeitarou/pf/portfolio/app/views/cards/index.html.haml where line #15 raised: undefined method `id' for nil:NilClass このようにエラーが出ています。 ターミナルの方では ActionView::Template::Error (undefined method `id' for nil:NilClass): 12: フロントエンド 13: %ul.sub 14: %li 15: = link_to "①HTML", card_category_path(1, @category.id) 16: %li 17: = link_to "②CSS", card_category_path(:card_id, 2) 18: %li app/views/cards/index.html.haml:15 このように出ています。
winterboum

2021/03/23 04:26

cards/index.html.haml でエラーがでてますから cards_controller の action Indexで@categoryが定義されていないから、です
退会済みユーザー

退会済みユーザー

2021/03/23 04:55

cards_controllerになります def index @category = Category.find(params[:id]) @cards = Card.all.page(params[:page]).per(6) end こちらのコードを書くと ``` ActiveRecord::RecordNotFound in CardsController#index Couldn't find Category without an ID ``` このようなエラーが出てきます。
winterboum

2021/03/23 07:48

params[:id] が ないのでしょう。 log みて Parameters 確認してみて
退会済みユーザー

退会済みユーザー

2021/03/23 09:16

logみたのですがParametersがないです ``` ActiveRecord::RecordNotFound (Couldn't find Category without an ID): app/controllers/cards_controller.rb:5:in `index' Started GET "/" for ::1 at 2021-03-23 13:44:06 +0900 Processing by CardsController#index as HTML Completed 404 Not Found in 11ms (ActiveRecord: 5.9ms | Allo ``` 正常に動いているところではlogを見るとParametersがしっかりあります。 ``` Started GET "/cards/card_id/categories/4" for ::1 at 2021-03-22 22:24:54 +0900 Processing by CategoriesController#show as HTML Parameters: {"card_id"=>"card_id", "id"=>"4"} Category Load (0.4ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 4 LIMIT 1 ↳ app/controllers/categories_controller.rb:5:in `show' Rendering categories/show.html.haml within layouts/application Card Load (0.6ms) SELECT `cards`.* FROM `cards` WHERE `cards`.`category_id` = 4 LIMIT 7 OFFSET 0 ↳ app/views/categories/show.html.haml:6 Rendered categories/show.html.haml within layouts/application (Duration: 3.5ms | Allocations: 865) Completed 200 OK in 35ms (Views: 31.4ms | ActiveRecord: 1.0ms | Allocations: 21010) ```
winterboum

2021/03/23 10:57

Started GET "/" ですから root to: 'cards#index' でIndexに行ってますね Started GET "/cards/card_id/categories/4"  の方は resources :cards do#7つのアクションの省略 resources :categories, only: [:show] で行ってますね。 params[:id]が無いときにどのcategryにするのか、もしくはcategory無指定の時の動作をどうするか をまず決めましょう
退会済みユーザー

退会済みユーザー

2021/03/24 00:53

質問のところに追加で画像を挿入させていただきました。 ご確認いただければ嬉しいです。 現段階は画像のように登録されている情報がなかったら何も表示させないようにしています。
winterboum

2021/03/24 03:21

「登録されていなけれb」ではないです。「カテゴリの指定がなかったら」、です
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問