2013-03-04 3 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 

View - учреждения/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 /> 

Действительно большой Листинг-учреждения и идентификационные записи на следующей строке.

ответ

0

Я делал это несколько раз сейчас, и в случае, если кто-либо сталкивается с подобной проблемой, моя проблема должна была иметь дело с View part. Серьезно случай с 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 /> 
Смежные вопросы