I am new in ruby on windows.. I am trying to use the link to method but it gives me an http://localhost:3000/products.2(expecting http://localhost:3000/products/2). I read some forum regarding to this problem, one of the answer that I read(source) is
"Have you try tweet_path and user_path ?
You want to access the show action. For that action, the model name must be singular in the *_path call."
View
<html>
<head>
<title>MY STORE!</title>
</head>
<body>
<h1><align="center"> WELCOME TO MY STORE</h1>
<table border = "1" width="100%">
<tr>
<td>ID</td>
<td>Name</td>
<td>Size</td>
<td>Price</td>
<td>Created At</td>
<td>Updated At</td>
<td>Action</td>
</tr>
<% @product.each do |p| %>
<tr>
<td><%= p.id %></td>
<td><%= p.name %></td>
<td><%= p.size %></td>
<td><%= p.price %></td>
<td><%= p.created_at.strftime("%B, %d, %Y") %></td>
<td><%= p.updated_at %></td>
<td><%= link_to 'View', products_path(p) %></td>
</tr>
<% end %>
</table>
</body>
</html>
NOTE: I tried to make this singular <%= link_to 'View', product_path(p) %> and it gives me an error
NoMethodError in Products#index
undefined method `product_path' for #<#<Class:0x9955218>:0x4ca3b98>
Did you mean? products_path
<td><%= p.created_at.strftime("%B, %d, %Y") %></td>
<td><%= p.updated_at %></td>
<td><%= link_to 'View', product_path(p) %></td> // to this line
</tr>
<% end %>
Routes
Rails.application.routes.draw do
get 'products/' => 'products#index'
get 'products/:id' => 'products#show'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Controller
class ProductsController < ApplicationController
def index
@product = Product.all.order('created_at DESC')
end
def show
@post = Product.find(params[:id])
end
end