2013-03-04 38 views
0

我遇到了記錄未從數據庫返回的問題。沒有錯誤被拋出,但也許我錯過了簡單的東西。我將我的數據庫yml設置爲開發,測試和生產到同一個東西,以確保我的目標是數據庫。簡單導軌3查詢未加載數據

直接從數據庫查詢返回3個機構。

型號 - institution.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 

控制器 - institutions_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 /> 

結果 真的很大上市機構和Id記錄在下一行。

回答

0

我已經做了幾次現在,如果有人遇到過類似的問題,我的問題必須處理查看部分。這裏是嚴重的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 />