初学者,嵌入在html页面上的Ruby,没有出现在浏览器中?

时间:2014-05-08 08:04:31

标签: ruby ruby-on-rails-4

当推送到开放式转移时,我无法在浏览器上显示数据库的结果,而在我的localhost机器上它可以正常工作,有人知道那里会发生什么?

CONTROLER

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

<h1>index</h1>
   <% @post.each do |p|  %>
   <br />
   <%= p.email %>
   <%= p.title %>
 <% end %>

out put:

 index

 []

3 个答案:

答案 0 :(得分:1)

ERB有不同类型的标签。主要是:

  1. <% ruby code %>,在上下文中执行操作,
  2. <%= ruby code %>,它在上下文中执行操作并输出其结果
  3. <%# anything %>,插入评论。
  4. 您通常使用2在页面上编写内容,可以单独编写,也可以在使用1定义的块或循环内编写内容 - 看看我在上下文中说时的意思/ em>的

    您的代码应为:

    <% @post.each do |p|  %>
      <br />
      <%= p.email %>
      <%= p.title %>
    <% end %>
    

    除此之外,您获得的输出[]是第一次操作的结果:<%= @post.each do |p| %>。这可能意味着你有一个空列表。

答案 1 :(得分:0)

<%= @post.each do |p| %>

错了

删除“=”

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

答案 2 :(得分:0)

您不需要在html页面中使用等号来表示循环

替换此行

<%= @post.each do |p|  %>

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

并确保您的邮政控制器中会有一些记录 你会得到这样的空数组:

index
[]