I am trying to link from my set index page to card index directly without having to go through the show index. I am also concerned that my routes may be wrong due to a foreign key missing in my schema. I have added card_id manually to my set model. I will put all the relevant files below.
ActiveRecord::Schema[7.0].define(version: 2022_12_04_135913) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "pokemon_cards", force: :cascade do |t|
t.string "name"
t.string "set"
t.string "artist"
t.integer "price"
t.boolean "owned"
t.boolean "needed"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "pokemon_sets", force: :cascade do |t|
t.string "name"
t.string "series"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "card_id"
t.string "image_url"
t.string "symbol_url"
end
end
The below line which has a comment has the following error :
No route matches {:action=>"index", :controller=>"pokemon_cards", :pokemon_set_id=>nil}, missing required keys: [:pokemon_set_id]
<h1>Pokemon Sets</h1>
<ul>
<% @pokemon_sets.each do |set| %>
<li><%=link_to(set.name, pokemon_set_pokemon_cards_path(@pokemon_set)) %></li> # this throws an error
<li><%= set.series %></li>
<li><%= image_tag(set.image_url, size: "250x150") %></li>
<% end %>
</ul>
I will put below my nested resources in my routes file and also my routes in the terminal.
Rails.application.routes.draw do
root to: "pages#home"
resources :pokemon_sets, only: [:index, :show], path: 'sets' do
resources :pokemon_cards, only: [:index, :show], path: 'cards'
end
end
Prefix Verb URI Pattern Controller#Action
root GET / pages#home
pokemon_set_pokemon_cards GET /sets/:pokemon_set_id/cards(.:format) pokemon_cards#index
pokemon_set_pokemon_card GET /sets/:pokemon_set_id/cards/:id(.:format) pokemon_cards#show
pokemon_sets GET /sets(.:format) pokemon_sets#index
pokemon_set GET /sets/:id(.:format) pokemon_sets#show
class PokemonSetsController < ApplicationController
before_action :find_pokemon_set, only: [:show]
def index
@pokemon_sets = PokemonSet.all
end
def show
end
private
def find_pokemon_card
@pokemon_card = PokemonCard.find(params[:card_id])
end
def find_pokemon_set
@pokemon_set = PokemonSet.find(params[:id])
end
def pokemon_sets_params
params.require(:pokemon_cards).permit(:name, :series, :card_id)
end
end
I have tried a few things, like specifiying the controller in the link to to use the pokemon_cards controller but that did not work.
Is not a problem with your schema, the routes does not know what is going on with schema.
Are there some problems:
pokemon_set_idinside pokemon_card model no, the other way around.<%=link_to(set.name, pokemon_set_pokemon_cards_path(pokemon_set_id: @pokemon_set.id)) %>But is better try to re-write some parts of the code following the post in the second topic.