0

我遇到了一个问题,即没有从数据库返回记录。没有错误被抛出,但也许我错过了一些简单的东西。我将用于开发、测试和生产的数据库 yml 设置为相同的内容,只是为了确保我的目标是有问题的数据库。

直接从数据库中查询返回 3 个机构。

模型-机构.rb

class Institution < ActiveRecord::Base
  # attr_accessible :title, :body
  has_many :childinstitutions, :class_name => "Institution",
      :foreign_key => "parentinstitution_id"
  belongs_to :parentinstitution, :class_name => "Institution"

  def self.search(term)
    if term
      Institution.where('institutionname LIKE ?', :term).all
    else
      Institution.all
    end
  end
end

控制器 - 机构_controller.rb

class InstitutionsController < ApplicationController
  def index
    @institutions = Institution.search(params[:term])

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @institutions }
    end
  end

  def show
    @institution = Institution.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @institution }
    end
  end

  def new
    @institution = Institution.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @institution }
    end
  end
end

查看 - 机构/index.html.erb

<h1>Listing Institutions</h1>

<table>
  <tr>
    <th>Id</th>
    <th><% @institutions.length %> records</th>
  </tr>

<% for institution in @institutions %>
  <tr>
    <td><% institution.id %></td>
    <td></td>
  </tr>
<% end %>
</table>

<br />

结果 A 非常大的Listing Institutions和Id 记录在下一行。

4

1 回答 1

0

我现在已经这样做了几次,如果有人遇到类似的问题,我的问题必须处理View 部分。这里是 RTFM 的一个严重案例。要显示到屏幕的引用字段应具有 <%= 而不是 <%。

更新视图

<h1>Listing Institutions</h1>

<table>
  <tr>
    <th>Id</th>
    <th></th>
    <th><%= @institutions.length %> records</th>
  </tr>

<% for institution in @institutions %>
  <tr>
    <td><%= institution.id %></td>
  </tr>
<% end %>
</table>

<br />
于 2013-03-04T23:24:43.323 回答