2012-04-11 48 views
0

说我创建一个使用此表的人:导轨 - 创建一个数据库条目

rails new app; cd app; rails g scaffold Person name:string; rake db:migrate 

,然后尝试使用一排设置名称:

rails console 
Loading development environment (Rails 3.2.3) 
1.9.2p318 :001 > @person = Person 
=> Person(id: integer, name: string, created_at: datetime, updated_at: datetime) 
1.9.2p318 :002 > @person.name = "test" 
NoMethodError: undefined method `name=' for #<Class:0x007f9b8d807098> 

为什么最后一行失败,一个未定义的方法?

回答

3

您的@person = Person字面上是说@person是Person类。您可以使用@person = Person.new()。现在可以分配属性,因为实例变量是一个新的Person对象。

+0

Doh!当然。谢谢。 – Snowcrash 2012-04-11 22:17:11

+0

没有汗水的人!我们都有脑屁。 – graytist 2012-04-11 22:26:23