user4380892
user4380892

Reputation:

Link to user who created this post

I have the models Forum, Topic and Post, and I need to create a link to the user who created this post. This is my example:

- @posts.each do |post|
    tr
        td | user
        td = post.content
        td = post.created_at
        td = link_to post.user.login, user_path(@user) 

Topics controller:

def show
  @forum = Forum.find(params[:forum_id])
  @topics = Topic.find(params[:id])
  @posts = @topics.posts
  @user = @topics.user

All my relations seem to be okay.

Upvotes: 0

Views: 57

Answers (2)

Max Williams
Max Williams

Reputation: 32933

Your question is a bit confusing, partly due to variable names, and partly because you don't actually say what the problem is. But i think that you should be able to say

 td = link_to post.user.login, user_path(post.user) 

What i meant about variable names is that sometimes you use the plural form to refer to single objects, for example i would rewrite your show action as

def show
  @forum = Forum.find(params[:forum_id])
  @topic = Topic.find(params[:id])
  @posts = @topic.posts
end

and then in the view do

    - @posts.each do |post|
      tr
        td | user
        td = post.content
        td = post.created_at
        td = link_to post.user.login, user_path(post.user) 

Upvotes: 1

nsave
nsave

Reputation: 984

Maybe you want this:

td = link_to post.user.login, user_path(post.user) 

Upvotes: 0

Related Questions