意外的布局输出-RoR 3.0.1.1 [英] Unintended Layout Output - RoR 3.0.1.1

查看:87
本文介绍了意外的布局输出-RoR 3.0.1.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习RoR。我的问题是,当我在html.erb文件中调用该帖子时,得到了意外的输出。

I recently started learning RoR. My problem is I get an unintended output when I call the post in the html.erb file.

1 class TestSiteController < ApplicationController 
2   def Home 
3     @greeting = 'Hello' 
4     @posts = Post.all 
5
6     respond_to do |format|
7       format.html 
8       format.xml  { render :xml => @posts }
9       format.json { render :json => @posts }
10     end
11   end    


1 <h1>testsite#home</h1>
2 <%= link_to "contact", about_us_path %>
3 
4 <table>
5 <%= @posts.each do |p| %> 
6 <tr>
7   <td><%= p.topic %></td>
8   <td><%= p.post %> </td> 
9 </tr>
10 <% end %>
11 </table>

我在下面得到想要的帖子列表,但列表顶部是一系列帖子我不想输出。有人知道为什么会这样吗?

I get the list of posts I want below, but on top of the list is an array of posts I do not want outputted. Anyone know why this is happening?

下面是输出:

//Unwanted array
[#<Post id: 1, topic: "Rainy Day", post: "Today was a rainy day", created_at: "2012-03-29    21:22:43", updated_at: "2012-03-29 21:22:43">, #<Post id: 2, topic: "It works!", post: "Saving   data from the rails console to the mysql db ...", created_at: "2012-03-29 21:25:18", updated_at: "2012-03-29 21:25:18">]
//List start
Rainy Day   Today was a rainy day
It works!   Saving data from the rails console to the mysql db work! I'm so ecstatic! :D


推荐答案

您正在输出<$的内容c $ c> @posts ,因为您使用的是<%= 。您应该将其更改为 <%

You're outputting the contents of @posts because you're using <%=. You should change it to <%:

<% @posts.each do |p| %>

这篇关于意外的布局输出-RoR 3.0.1.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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