Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Couldn't find Category with 'id'=id

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<% @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.

>Solution :

you need to pass the id to the route. change from

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

to

<%=  link_to category_path(c.id) do %>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading