Rails: Redirect to user's profile inside comments

93 Views Asked by At

I have a pretty simple comment system in my rails app - I would like to allow user to clic on commenters name to access his profile.

But without any success - I am not sure what I am missing.

<% @comments.each do |comment| %>

          <p><a href="<%= user_path(@comment.user_id) %>" style="text-decoration : none"><%= image_tag comment.user.image, class: "img-circle", width: "30x30" if comment.user %><font class="small" color="#28c3ab"> @<%= comment.user.name if comment.user %></font></a> 

<% end %>

But I have a no routes match error:

No route matches {:action=>"show", :controller=>"users", :id=>nil} missing required keys: [:id]

Any ideas what I am missing? I Know it's something easy but I'm out of ideas for now. Where should I add this call in my controllers?

2

There are 2 best solutions below

3
mohameddiaa27 On BEST ANSWER

I figured out that your problem might be the @comment

change:

user_path(@comment.user_id)

to:

user_path(comment.user_id)
4
Simone Carletti On

Check the value of @comment.user_id. It's likely there is no user_id assigned to the instance and the value is nil.