如何访问特定的对象字段?

问题描述:

我正在构建基本的裸骨社交媒体应用程序。如何访问特定的对象字段?

我有一个用户类和一个状态类。

对于每种状态,都有一个“创建者”(一个用户对象)和一个“主题”(一个用户对象,状态是关于)。我能够通过使用acts_as_taggable_on创建标签。最终发生的事情是,当用户创建帖子时,他/她可以从下拉菜单中选择另一个用户。然后存储所选用户的id属性。

现在我试图链接到所选用户的个人资料。这是我在配置文件页面上显示状态的代码。

<% if @statuses %> 
     <% @statuses.each do |status| %> 
     <div class="well"> 
      <%= status.content %> 
      <br></br> 

      #link to user who's associated with the tagId 
      <%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %> 
      <hr /> 
      <%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago 
     </div> 
     <% end %> 
    <% end%> 

这就是上面的代码打破行

   <%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %> 

谁能帮我这个?

+0

定义“断码” –

+0

是很有帮助的发布错误信息 – Ren

不奇怪此行失败:

<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %> 

有几点:

  • 这是一个小吸尘器把它划分到多行
  • 我怀疑你的问题是因为你传递一个profile_name到user_profile_path而不是一个id,虽然我不能确定没有看到你的路线。

尝试以下操作:

<% profile_user = User.find(status.tag_list) %> 
<%= link_to profile_user.profile_name, user_profile_path(profile_user.id) %>