Search code examples
ruby-on-railsrubyprofile

How can I link a profile page in Ruby on Rails


I'm currently building a marketplace with Ruby on Rails. Right now I'm trying to build a profile page with devise (based on this tutorial: https://www.youtube.com/watch?v=BEkpwM-GvMQ) . For devise I've created the user model. For the profile page I've created the users controller. For the marketplace I've created the scaffold page(pages). Every user has a id and the profile page is basically based on that id. Does somebody know how I can link somebodys profile page in his navbar?

app/controllers/users_controller.rb

    class UsersController < ApplicationController


  def show
    @user = User.find(params[:id])
    @user_pages = @user.pages
  end

end

app/views/layouts/application.html.erb (basically the navbar)

    <!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
    <%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <header>
            <!--NAVBAR-->
    <nav class="navbar navbar-expand" style="background-color: #B1D3FE;">
        <div class="container-fluid">
          <a class="navbar-brand" href="../pages"><%= image_tag("navbar/logo.png") %></a>
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
          <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav me-auto ">
            </ul>

            <% if !user_signed_in? %>
            <li class="nav-item list-unstyled" >
                <%= link_to 'Login/Register', new_user_session_path, class:"btn", style:"background-color: #00188A; color: white;" %>
            </li>
            <% end %>

            <% if user_signed_in? %>
            <li class="nav-item dropdown list-unstyled me-5">
              <a class=" btn nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false" style="background-color: #00188A; color: white;">
              Profile
              </a>
                <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                    <li>
                      
                      <%= link_to "My Profile",  users_path , class:" dropdown-item" %>
                                      
                    </li>
                    <li>
                      <%= link_to "Edit Profile", edit_user_registration_path, class:" dropdown-item" %>
                    </li>
                    <li><hr class="dropdown-divider"></li>
                    <li>
                      <%= link_to "Settings", edit_user_registration_path, class:" dropdown-item" %>
                    </li>
                    <li>
                      <%= link_to "Logout", destroy_user_session_path, method: :delete, class:" dropdown-item" %>
                    </li>
                </ul>
            </li>
            <% end %>

          </div>
        </div>
      </nav>

  </header>

  <body>
       



<% if notice %>
          <%= render 'layouts/alerts' %>
<% end %>


 <%= yield %>

app/views/users/show.html.erb

    <%= @user.email %>

<% @user_pages.each do |pages| %>
    <%= link_to pages.title, page_path(pages) %>
<% end %>

app/config/routes.rb

Rails.application.routes.draw do
  devise_for :users
  resources :pages
  resources :users
  root to: 'pages#index'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

When I clicK on the My-Profile link on the navbar (<%= link_to "My Profile", users_path , class:" dropdown-item" %>) I land on this page: profile page bug

Thank you for reading. It would help me allot if somebody would know how to fix this.


Solution

  • The users_path links to the index page which shows all the users. The reason you are seeing the error is because you only have the show action setup in the controller. To link to a specific users profile page, you would link to the show path which would be user_path(@user). You need to pass the @user in order for rails to know which user to show on the page.

    The link would look something like this:

    <%= link_to "My Profile", user_path(@user), class:" dropdown-item" %>