Home > Net >  Couldn't find Category with 'id'=id
Couldn't find Category with 'id'=id

Time:09-29

When i click my link_to, it takes to http://localhost:3000/categories/id rather than http://localhost:3000/categories/1. When I put the 1 into the URL it then takes me to the correct page, but I want it to work through the link, and so i suspect it is a routing issue.

These are my routes

  resources :categories, only: [ :index, :show ] do
    resources :stocks, only: [ :index, :show ]
  end

  resources :stocks, only: [ :index, :show ] do
    resources :histories, only: [ :index ]
  end

My link to

<% @categories.each do |c| %>
  <%=  link_to category_path(:id) do %>
  <%= c.name %>
  <% end %>

And this is my categories controller

  def index
    @categories = Category.all
    @stocks = Stock.all
  end

  def show
    @category = Category.find(params[:id])
    @stock = Stock.find(params[:id])
    @categories = Category.where(id: @stocks)
    @stocks = Stock.where(category_id: @stock.id)
  end

Any help will be greatly appreciated, thank you.

CodePudding user response:

you need to pass the id to the route, that's the variable part of it. change from

<%=  link_to category_path(:id) do %>

to

<%=  link_to category_path(c.id) do %>
  • Related