D.Graves D.Graves - 1 year ago 68
Ruby Question

Linking to profile page by username on Ruby

What I want to be able to do is when someone clicks on a picture they will see the user who uploaded that pictures username and I want them to be able to click that username and go to that persons profile page.

Here is my issue I have it working for the first post. I do have multiple users and the posts show the correct username for each one. It links perfectly fine. But for any post after that it throws up an error and the user id gets higher for each post.

This is my profile page so the posts know that this user posts these pics. And when I click the first picture and the username it correctly takes me to here.

enter image description here

Now when I click the 2nd picture I get this. And if I click the 3rd pic I get couldn't find user with "id"=2. Even though the post recognizes who posted the pic by having the correct username.

enter image description here

This is the code I have on my show for the posts

<div class="panel-body">

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


Again it works perfectly fine for the 1st upload If you guys need to see more code I will gladly upload it

Answer Source

Could you try this ? ( user_path(@post.user.id) )

<p><strong><%= link_to(@post.user.username.capitalize, user_path(@post.user.id)) if @post.user %></strong></p>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download