初学者,嵌入在 html 页面上的 Ruby 没有显示在浏览器中? [英] Beginner, Ruby embedded on html page, doesn't show up in browser?
问题描述
当我将浏览器推到 open shift 时,我无法在浏览器上看到数据库的结果,虽然它在我的 localhost 机器上工作正常,但有人知道那里发生了什么吗?
I can not the results of database on browser when pushing it to open shift,while it works okay on my localhost machine, some one knows what is going there?
控制器
class PostController < ApplicationController
def index
@post = Post.all
end
def show
end
def new
end
def edit
end
def delete
end
end
型号
class Post < ActiveRecord::Base
attr_accessible :email, :text, :title
end
index.html.erb
index.html.erb
<h1>index</h1>
<% @post.each do |p| %>
<br />
<%= p.email %>
<%= p.title %>
<% end %>
输出:
index
[]
推荐答案
ERB
有不同种类的标签.主要有:
ERB
has different kind of tags. The main ones are:
<% ruby code %>
,在上下文中执行操作,<%= ruby code %>
,它在上下文中执行一个操作并输出其结果,<%# nothing %>
,插入注释.
<% ruby code %>
, which executes an operation in context,<%= ruby code %>
, which executes an operation in context and outputs its result,<%# anything %>
, which inserts a comment.
您通常使用 2
在页面上编写内容,可以单独编写,也可以在使用 1
定义的块或循环内编写 -- 明白我说的意思在上下文中?
You typically use 2
to write things on the page, either on their own or inside a block or loop defined with 1
-- see what I meant when I said in context?
您的代码应该是:
<% @post.each do |p| %>
<br />
<%= p.email %>
<%= p.title %>
<% end %>
<小时>
除此之外,您得到的输出 []
是第一个操作的结果:<%= @post.each do |p|%>
.这可能意味着您有一个空列表.
aside from that, the output you are getting, []
, is the result of the first operation: <%= @post.each do |p| %>
. It might mean that you've got an empty list.
这篇关于初学者,嵌入在 html 页面上的 Ruby 没有显示在浏览器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!