显示属于 Rails 中特定条目的所有表条目 [英] Show all the table entries that belong to a specific entry in Rails

查看:48
本文介绍了显示属于 Rails 中特定条目的所有表条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 rails 应用程序,我必须建模;订阅和帖子.每个帖子属于一个子,一个子有很多帖子.在 Sub show 视图中,我想列出它拥有的所有帖子标题.

I am working on a rails app and I have to models; Subs and Posts. Each post belongs to a sub, and a sub has many posts. In the Sub show view I want to list out all the titles of posts that it has.

目前我有

<%= @sub.posts.each do |p|%>
  <%= p.title %>
<% end %>

但是当我运行时它显示:

But when I run that it shows:

Test [#Post id:2, title: "Test", created_at: "2015-07-29 01:49:58",updated_at: "2015-07-29 01:49:58", user_id: 1, sub_id: 1>]

Test [#Post id:2, title: "Test", created_at: "2015-07-29 01:49:58", updated_at: "2015-07-29 01:49:58", user_id: 1, sub_id: 1>]

我希望它只显示测试,而不是整个表条目

I want it to just show Test, instead all the whole table entry

谢谢!

推荐答案

这是因为你在 each 语句的行中使用了 <%= ,这意味着结果被输出到HTML,而不是 <% 没有.each 调用的结果是一个枚举器对象,它是您在标题后看到的输出.正确的代码是:

This is because you're using <%= in the line with the each statement, which means the result is outputted to the HTML, instead of <% which does not. The result of an each call is an enumerator object, which is the output you see after the title. The correct code would be:

<% @sub.posts.each do |p|%>
  <%= p.title %>
<% end %>

这篇关于显示属于 Rails 中特定条目的所有表条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆