想請教各位前輩一個關於資料庫連結的問題

32 views
Skip to first unread message

Keane

unread,
Nov 4, 2008, 8:29:21 PM11/4/08
to Ruby on Rails Taiwan
如題,我這幾天再研究RUBY.
大致瞭解了ROR的運作,但是對於資料庫的連接一直很困惑.
如果不用rake,要用我自建的資料表,是否資料表名稱與controller相同就好?
我試著做,但是一直出現如下的問題.
按判斷應該是沒有連到資料庫,所以用迴圈跑資料庫內容時是nil,
內容如下 :

NoMethodError in Emplyees_control#index

Showing emplyees_control/index.html.erb where line #2 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Extracted source (around line #2):

1: 111
2: <%for emp in @emp -%>
3: <%=emp.name%>
4: <%=emp.password%>
5: <%=emp.account%>

RAILS_ROOT: c:/ruby/www/web1

感謝. :D

CFC

unread,
Nov 4, 2008, 8:48:10 PM11/4/08
to Ruby on Rails Taiwan
跟資料庫有關的東西最主要是Model
所以是跟Model Name相同,並且資料表名是Model Name的複數形態
不過你也可以在Model中用set_table_name去指定你的資料表
假設你有一個資料表叫做poor_guy,有一個Model叫做GoodGuy,你可以在good_guy.rb中寫這個:

class GoodGuy < ActiveRecord::Base
set_table_name "poor_guy"
end

然後重新啟動Server即可

Controller跟資料庫沒有直接關係

Keane

unread,
Nov 4, 2008, 10:04:42 PM11/4/08
to Ruby on Rails Taiwan
還是出問題.....冏
這兩天都遇到這兩個問題...
可以麻煩各位大大嗎?
我的設置流程如下
rails -d mysql webs
webs> ruby script\generate model web1
webs> ruby script\generate controller web1

controller web1.rb如下 :
class Web1Controller < ApplicationController
def index
@emp= Web1.getall
end
end

model web1.rb如下 :
class Web1 < ActiveRecord::Base
set_table_name "web1"
def self.getall
find(:all, :order=>"name")
end

end

index.html.erb如下 :
<%for emp in @emp -%>
<%=@emp.name%>
<%=@emp.adddress%>
hihi

<%end%>


可是出現的訊息卻如下.
NameError in Web1Controller#index

uninitialized constant ActiveRecord

RAILS_ROOT: c:/ruby/www/webs
Application Trace | Framework Trace | Full Trace

c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_support/
dependencies.rb:279:in `load_missing_constant'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_support/
dependencies.rb:468:in `const_missing'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_support/
dependencies.rb:480:in `const_missing'
app/models/web1.rb:1
app/controllers/web1_controller.rb:4:in `index'


CFC

unread,
Nov 5, 2008, 8:24:04 AM11/5/08
to Ruby on Rails Taiwan
好神奇
居然找不到ActiveRecord..
你怎樣裝Rails的

Keane

unread,
Nov 5, 2008, 9:52:12 AM11/5/08
to Ruby on Rails Taiwan
我先裝RUBY包阿~ 官網下載的.
然後打
gem install update
gem install rails.
gem install mysql
gem install mongrel
之類的

原來這是找不到ActiveRecord.
我以為是程式打錯他無法初始化ActiveRecord.
那大大是建議我重裝嗎? 還是?

On 11月5日, 下午9時24分, CFC <zuso...@gmail.com> wrote:
> 好神奇
> 居然找不到ActiveRecord..
> 你怎樣裝Rails的
>
> On 11月5日, 上午11時04分, Keane <hitake...@gmail.com> wrote:
>
> > 還是出問題.....冏
> > 這兩天都遇到這兩個問題...
> > 可以麻煩各位大大嗎?
> > 我的設置流程如下
> > rails -d mysql webs
> > webs> ruby script\generate model web1
> > webs> ruby script\generate controller web1
>
> > controller web1.rb如下 :
> > class Web1Controller < ApplicationController
> > def index
> > @emp= Web1.getall
> > end
> > end
>
> > model web1.rb如下 :
> > class Web1 < ActiveRecord::Base
> > set_table_name "web1"
> > def self.getall
> > find(:all, :order=>"name")
> > end
>
> > end
>
> > index.html.erb如下 :
> > <%for emp in @emp -%>
> > <%...@emp.name%>
> > <%...@emp.adddress%>

Keane

unread,
Nov 6, 2008, 11:00:38 AM11/6/08
to Ruby on Rails Taiwan
都沒人要救我嗎?? ><"
已經第三天了~還是找不出方法....

我今天有將RUBY重裝,
再裝gem,再
gem update
gem install rails
然後一樣

rails -d mysql webs
webs> ruby script\generate model web1
webs> ruby script\generate controller web1

<<<controller web1.rb>>> 如下 :

class Web1Controller < ApplicationController
def index
@emp= Web1.getall
end
end

<<<model web1.rb>>> 如下 :

class Web1 < ActiveRecord::Base
set_table_name "web1"
def self.getall
find(:all, :order=>"name")
end
end

<<<index.html.erb>>>如下 :

<%for emp in @emp -%>
<%...@emp.name%>
<%...@emp.adddress%>
hihi
<%end%>

database.yml
改成資料庫名稱,打上密碼.

enviroment.rb只移除這行註解 :

config.frameworks -=
[ :active_record, :active_resource, :action_mailer ]

有誰能幫一下........ ><

CFC

unread,
Nov 8, 2008, 1:35:30 AM11/8/08
to Ruby on Rails Taiwan
你幹嘛移除那行註解
難怪找不到Orz
Reply all
Reply to author
Forward
0 new messages