No route matches {:action=>"show", :controller=>"carts"} missing required keys: [:id]

Hello,

I follow the Agile Web development book.

Now as a extra challenge I try to make the card visible on a seperate
page.

So I made this link :

  • <%= link_to ' Card'.html_safe, cart_path %>
  • But now I see the above error message.
    When I do card_path (:id) I see a message that there is no card with the
    id
    of id.

    Roelof

    What is the url you’re using to show this on a separate page ?

    if its in the pattern of “/cards/:id/something” then you dont have to
    provide the card path as rails picks up @card object to build the path
    or
    you’ll have to pass the card variable inside the path method.

    eg

    <% @cards.each do |card| %>

  • <%= link_to ' Card'.html_safe, cart_path(card) %>
  • <% end %>

    This way, rails would know “which” exact card this link should take them
    to.

    Hello,

    It’s is the form of /cards/:id

    I tried

  • <%= link_to ' Card'.html_safe, cart_path(cart) %>
  • <%= link_to ' Card'.html_safe, cart_path(card) %>
  • but on both I see this message :

    undefined local variable or method `cart’

    undefined local variable or method `card’

    Roelof

    Op maandag 8 december 2014 14:23:33 UTC+1 schreef Vivek S.:

    If you’re on “/cards/:id”, then look at your cards_controller.rb ,
    you’d
    see @card defined , hence you should use

  • <%= link_to ’ Card’.html_safe, cart_path(@card) %>
  • But why would you goto to the show page ( /cards/:id/ ) when you’re
    already
    on the page ? regarding the cart, You have to follow the same procedure.
    define @cart in the controller/action and use it like this

  • <%= link_to ' Card'.html_safe, cart_path(@cart) %>
  • If you can give more info on what you’re trying to do (app structure) ,
    We
    can help you a bit better :slight_smile:

    PS: i haven’t read the book

    Cheers
    Vivek

    Im at the home page and I want to make a link to /card/:id

    According to rake routes I have to use cart_path.

    The carts route looks like this :

    class CartsController < ApplicationController

    before_action :set_cart, only: [:show, :edit, :update, :destroy]

    def index
    @carts = Cart.all
    end

    def show
    end

    def new
    @cart = Cart.new
    end

    Which uses this file :

    module CurrentCart
    extend ActiveSupport::Concern

    private
    
    def set_cart
        @cart = Cart.find(session[:cart_id])
        rescue ActiveRecord::RecordNotFound
        @cart = Cart.create
        session[:cart_id] = @cart.id
    end
    

    end

    So I have to figure out how to make the id of the card avaible on the
    url.

    Roelof

    Op maandag 8 december 2014 14:38:20 UTC+1 schreef Vivek S.: