<% @products.each do |product| %> <%= link_to 'Show', product %> <%= link_to 'Edit', edit_product_path(product) %> <%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %> <% end %> <%= link_to 'Edit', edit_product_path(@product) %> | <%= link_to 'Back', products_path %> 

Why the product_path is not used for show and destroy , and in other cases the path used

    3 answers 3

    If you pass a resource as a parameter to the link_to rail helper (for example, an ActiveRecord object), the helper will build the path to it. To do this, link_to passes the parameters to the url_for method, which builds a path using the specified parameters.

    • For example: <%= link_to 'Show', product %> returns the path /products/1 , where 1 is the id resource.
    • And if you specify an array of the form [:admin, product] - this will create a link to /admin/products/1 .
    • And if - an array of the form [:admin, :products] - creates a link to /admin/products .
    • Similarly, you can also set links in controllers, for example, redirect_to [:edit, product]

    As a result, you can replace in your views ...

     <%= link_to 'Edit', edit_product_path(@product) %> | <%= link_to 'Back', products_path %> 

    ...on...

     <%= link_to 'Edit', [:edit, @product] %> | <%= link_to 'Back', :products %> 

    ... and the result will not change.

      The routes issued by the rail helper resources can be divided into two groups:

      Direct actions (the request with which is meaningful from the point of view of HTTP semantics):

      • index: GET collections
      • create: POST resource
      • show: GET resource
      • update: PATCH resource (or PUT, although the correctness of the Rails semantics is questionable here)
      • destroy: resource delete

      ... and auxiliary stubs, which are needed solely because "you need to tell the browser how to perform the action":

      • new: interface to create
      • edit: interface to update

      Direct actions simply use the path to what the action is being performed on, and the action itself is recognized from the HTTP method.

      But there are no meaningful HTTP methods for stubs , so they use "with a tail" paths. And since the helper returns only the path (but not the method!), For direct actions, the path can be collected directly from their resource object , and for stubs, a helper or an object with a "hint" is needed.

      Since the form of “hints” in the routing system is little known and has a very obscure syntax, it is often avoided in favor of helpers, but this is a matter of style.

        In general, the link_to link_to takes a path that can be obtained with the help of path-helper. However, when a single object is being edited or deleted, ActionPack can automatically form a path. Nothing is stopping you from using a heleper and in this case

         <%= link_to 'Show', product_path(product) %> 

        However, it turns out longer.

        • No, not all, for example, products_path - cheops sep