現状
フロントエンド %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
画像の追加
回答1件
あなたの回答
tips
プレビュー