Since only logged in users can create products, we can modify the view to only display the new product link if the user is authenticated.

<%= link_to "New product", new_product_path if authenticated? %>

Click the Log out button and you’ll see the New link is hidden. Log in at http://localhost:3000/session/new and you’ll see the New link on the index page.

Optionally, you can include a link to this route in the navbar to add a Login link if not authenticated.

<%= link_to "Login", new_session_path unless authenticated? %>

You can also update the Edit and Delete links on the view to only display if authenticated.

<h1><%= @product.name %></h1>
<%= link_to "Back", products_path %>
<% if authenticated? %>
<%= link_to "Edit", edit_product_path(@product) %>
<%= button_to "Delete", @product, method: :delete, data: { turbo_confirm: "Are you sure?" } %>
<% end %>
Proudly built by Evil Martians based on the Rails Guides.
Files
Preparing Environment
  • Preparing Ruby runtime
  • Prepare development database