D.Graves
D.Graves

Reputation: 189

Link to User Profile Page By Username

I know this question has been asked a ton of times but I just can't seem to find the answer Im looking for that helps me. So I have created User's profile pages and they work perfectly fine. When I type in the User's Id manually for instance http://localhost:3000/users/3 that specific User's profile page shows up. Now Im looking to be able to link to that current user profile page in my header when that person logs on and looking to be able to link to anyones profile page when they click on the person's username on a post.

This is the error I get from my header when I have the line of code of

<%= link_to "Profile", user_path %>

enter image description here

These are my routes

resources :posts
devise_for :users
resources :users, :only => [:show]

This is my UsersController

class UsersController < ApplicationController
 def show
  @user = User.find(params[:id])
  @user_posts = @user.posts
 end
end

And this is my section where I have the username on the posts

<div class="panel-body">

    <p><strong><%= @post.user.username if @post.user %></strong></p>
    <p><%= @post.description %></p>
    <% if @post.user == current_user %>
     <%= link_to 'Edit', edit_post_path(@post) %>
    <% end %>

</div>

If you guys need anymore information I will gladly write down more information. And again what Im looking to do is to be able to have a link to the current user profile page in the header when they log on and to be able to click on the username on any post to see that user's profile.

Here are my routes

enter image description here

Upvotes: 0

Views: 947

Answers (1)

George Chen
George Chen

Reputation: 6949

It expects an id for that route

<%= link_to "Profile", user_path(current_user) %>

Upvotes: 1

Related Questions