【问题标题】:Michael Hartl - Chapter 2迈克尔·哈特尔 - 第 2 章
【发布时间】:2016-08-03 22:28:28
【问题描述】:

我一直在阅读第 2 章,进展顺利,只是他要求读者向用户展示第一个微博,这似乎很简单。可以看章节here

在我的show.html.erb 中,我添加了以下几行:

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @user.name %>
</p>

<p>
  <strong>Email:</strong>
  <%= @user.email %>
</p

<p>
  <strong>Post:</strong>
  <%= @user.microposts.first %>
</p>

<%= link_to 'Edit', edi<p id="notice"><%= notice %></p>

但我在 /users/1 看到的只是“帖子:#”。

知道我做错了什么吗?

谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    &lt;%= @user.microposts.first %&gt; 返回单个帖子对象。渲染内容这样做

    &lt;%= @user.microposts.first.content %&gt;

    【讨论】:

    • 嗯,这很简单。谢谢。
    • 或者你可以覆盖to_s,所以帖子的表示就是它的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2015-07-07
    相关资源
    最近更新 更多