嗨,我有一個初學者到軌道和我做的似乎是一個常見的用例,但它的有趣,它似乎很難在Rails中,並沒有在互聯網上關於該主題的帖子:取代db/development.sqlite3與db/Listings.sqlite3(prepopulated數據庫)得到錯誤:undefined方法
我創建了一個新的rails應用程序。由於我有一箇舊數據庫,我想包括在我的新rails應用程序中,我用舊數據庫「Listings.sqlite3」替換了「development.sqlite3」
然後我生成了一個模型「business.rb」匹配我的傳統數據庫中的「企業」表。下面是它的代碼:
class Business < ActiveRecord::Base establish_connection "Listings_development" end
然後我修改了「配置/ database.yml的」文件,以包括以下內容:
Listings_development: adapter: sqlite3 database: db/Listings.sqlite3 pool: 5 timeout: 5000 Listings_test: adapter: sqlite3 database: db/Listings.sqlite3 pool: 5 timeout: 5000 Listings_production: adapter: sqlite3 database: db/Listings.sqlite3 pool: 5 timeout: 5000
之後我生成稱爲控制器「businesses_controller .RB 「其具有以下代碼:
class BusinessesController < ApplicationController def show end def index @businesses = Business.all respond_to do |format| format.html #index.html.erb end end end
最後我改變了」 配置/路由.RB 「文件如下:
Directory::Application.routes.draw do resources :businesses get "business/index" match ':controller(/:action(/:id))(.:format)'
最後我加了一個視圖文件」 index.html.erb:
<h1>Listing businesses</h1> <table> <tr> <th>Name:</th> <th>Phone Number:</th> <th>Address:</th> </tr> <% @businesses.each do |business| %> <tr> <td><%= business.company_name %></td> <td><%= business.phone_number %></td> <td><%= business.address %></td>
我以爲我做了,我已經成功地管理將我的遺留數據庫及其數據整合到我的新Rails應用程序中。但是當我在瀏覽器窗口中輸入「http://localhost:3000/businesses/」時。我在屏幕上得到這個錯誤:
Showing /Users/AM/Documents/RailsWS/cmdLineWS/Directory/app/views/businesses/index.html.erb
where line #14 raised:
undefined method `company_name' for #<Business:0x00000104e1d0e8>
Extracted source (around line #14):
11:
12: <% @businesses.each do |business| %>
13: <tr>
14: <td><%= business.company_name %></td>
15: <td><%= business.phone_number %></td>
16: <td><%= business.address %></td>
17: <td><%= link_to 'Show', business %></td>
Rails.root: /Users/AM/Documents/RailsWS/cmdLineWS/Directory
什麼與Rails?包含遺留數據庫是否真的很複雜?我認爲鐵軌的全部重點都是快速原型。
這裏是我的Listings.db的快照文件
順便說一句,該是構建Web應用程序「的Rails的整點」,而在我看來的話「快速原型」和「遺留數據庫」並不屬於同一段。 – 2012-03-09 19:27:19
您的企業表的結構如何? – 2012-03-09 19:31:54
剛剛添加了我的Listings.db的圖像,其中包含Businesses表,這些列是:「公司名稱」,「地址」,「套件號 」,「城市」,「省」,「電話號碼」,「郵政代碼「,」傳真號碼「,」緯度「,」經度「,」網站「 謝謝 – banditKing 2012-03-09 19:36:59