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

时间:2016-07-14 16:28:23

标签: ruby-on-rails ruby attributes associations ruby-on-rails-5

我正在构建一个基本的简单社交媒体应用程序。

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

对于每个状态,都有一个" creater" (用户对象)和"主题" (状态所在的用户对象)。我可以使用acts_as_taggable_on gem创建标签。最终发生的事情是当用户去创建帖子时,他/她可以从下拉菜单中选择另一个用户。然后存储所选用户的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) %>

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这条线路失败并不奇怪:

<%= 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) %>