0

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
Community
  • 1
  • 1
Angel
  • 966
  • 4
  • 25
  • 60

2 Answers2

1

Try to change your routes:

get 'products/' => 'products#index'
get 'products/:id' => 'products#show'

To:

resources :products
oj5th
  • 1,379
  • 10
  • 25
  • Is it mandatory to use resources? What if I dont want to use it for example, I mean isn't the same with that? – Angel Apr 05 '17 at 03:09
  • No, it's not mandatory but it creates seven different routes in your application, all mapping to the Products controller. http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions – Kavincat Apr 05 '17 at 03:14
  • @Angel, ideally you should add `resources` and if you only want to use specific route only, you can use `:only` on your routes. Example: `resources :products, only: [:show]` – oj5th Apr 05 '17 at 03:16
  • @Kavincat I also read the guide, and I also check syntax of resources(through http://localhost:3000/info/routes).. GET /products/:id(.:format) products#show it is also the same with my routes gets '/products/:id => 'products#show' – Angel Apr 05 '17 at 03:22
1

Actually the main issue here is the following routes will not generate the route helper

get 'products/' => 'products#index'
get 'products/:id' => 'products#show'

so even if the URL and controller is correct you can't use product_path method

To use that there are two ways

resources :products, only: [:index, :show]

or

get 'products/' => 'products#index', as: 'products'
get 'products/:id' => 'products#show', as: 'product'

And now you can use products_path and product_path(10)

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88