Before getting into details I have read through these posts to try to find the solution without success : one, two, three
That being said: I am [new and] building an ecomm site for selling secondhand clothing, shoes and decor items.
My structure has only one Product
model and associated controller and table. Each 'product' has one of three different main categories, which is what I am using to differentiate and create 3 different URLs.
My routes look like this:
Rails.application.routes.draw do
root to: 'pages#home'
get 'clothing', to: 'products#clothing'
get 'clothing/:id', to: 'products#show'
get 'shoes', to: 'products#shoes'
get 'shoes/:id', to: 'products#show'
get 'home', to: 'products#home'
get 'home/:id', to: 'products#show'
get 'products/new', to: 'products#new'
post 'products', to: 'products#create'
end
My products_controller
looks like this:
class ProductsController < ApplicationController
before_action :set_all_products
before_action :set_one_product, only: [:show]
def shoes
@all_shoe_products = @all_products.where(main_category_id: MainCategory.find_by_name("shoes").id)
end
def clothing
@all_clothing_products = @all_products.where(main_category: MainCategory.find_by_name("clothes").id)
end
def home
@all_home_products = @all_products.where(main_category: MainCategory.find_by_name("housewares").id)
end
def show
end
def new
@new_product = Product.new
end
private
def set_one_product
@product = Product.find(params[:id])
end
def set_all_products
@all_products = Product.all
end
end
And when writing <%= link_to clothing_path(product) %>
('product' being the placeholder in an .each
loop), I get a path: root/clothing.[:id]
and not root/clothing/[:id]
I know I am making a convention error, and trying to have 3 different URLs within the same controller may be where I am gong wrong.
Note: manually entering root/clothing/[:id]
in the address bar does return a product correctly.