Annette
Annette

Reputation: 317

Link to user profile

I'm trying to create a link that will direct people to the users profile page by clicking on their name when they look at their pics. The line of code is below however, I am not sure how to turn this into a link that will direct people to the users profile. Right now the users post displays their name in association with their post. Hopefully this makes sense Thanks in advance.

<p><strong><%= @post.user.name if @post.user %></strong></p>

The route to the users profile page is users_path.

Post/Show.html

<div class="row">
    <div class="col-md-offset-4 col-med-8">
        <div class="panel panel-default">
        <div class="panel-heading center">

        <% if @post.image.url %>
            <%= image_tag @post.image.url(:medium) %>

        <% elsif @post.video.url %>
            <%= video_tag @post.video.url(:medium), controls: true, type: "video/mp4" %>
        <% end %>

        </div>
        <div class="panel-body">
        <p><%= @post.description %></p>
        <p><strong><%= @post.user.name if @post.user %></strong></p>

        <% if @post.user == current_user %>
            <%= link_to edit_post_path(@post) do %>
            <span class="glyphicon glyphicon-edit"></span>
            Edit
          <% end %>
        <% end %>
        <%= link_to 'Back', posts_path %>
        </div>
</div>

Upvotes: 1

Views: 1974

Answers (2)

AaronG
AaronG

Reputation: 84

I did this like this... <%= link_to (@post.user.name), user_path(@post.user) %>

Upvotes: 0

RAJ
RAJ

Reputation: 9747

Considering user_path is path to user's profile

<p>
  <strong>
    <%= link_to(user.name, user_path(user)) if user = @post.user %>
  </strong>
</p>

Upvotes: 1

Related Questions