2013-01-21 49 views
0

我刚刚从实用程序员的敏捷Web开发与Rails的Ruby on Rails学习。在rails上的语法错误ruby

我已经写了一个小程序,它运行时,我收到下面的语法错误,当我运行它:

/Users/colinlabri/Desktop/depot/app/models/product.rb:2: syntax error, unexpected ':', expecting keyword_end 
    attr_accessible : title, :description, :image_url, :price 
       ^
/Users/colinlabri/Desktop/depot/app/models/product.rb:2: syntax error, unexpected ',', expecting tCOLON2 or '[' or '.' 
    attr_accessible : title, :description, :image_url, :price 
             ^
Rails.root: /Users/colinlabri/Desktop/depot 

Application Trace | Framework Trace | Full Trace 

app/controllers/products_controller.rb:1:in `<top (required)>' 

为DB的代码如下:

class CreateProducts < ActiveRecord::Migration 
    def change 
    create_table :products do |t| 
     t.string : title 
     t.text :description 
     t.string :image_url 
     t.decimal :price, precision: 8, scale: 2 

     t.timestamps 
    end 
    end 
end 

版本如下所示: ruby​​ 1.9.3p362 Rails 3.2.11

我应该检查我的sqlite安装吗?

回答

1

你只需要修复这条线:

t.string : title 

到:

t.string :title 

你的模式也有在attr_accessible调用了同样的问题。

+0

感谢马克,我应该知道!我已经改变了上述内容,现在我保存了文件,找不到表'产品' – user1900791

+0

您的迁移很可能无法运行,或者您之前没有运行过。从你的终端尝试'rake db:migrate'。 –

0

:something实际上是Ruby中的符号。您不能在:和符号名称之间留下任何空格。

将迁移文件中的t.string : title更改为t.string :title

而在你的产品型号,

变化attr_accessible : titleattr_accessible :title

+0

谢谢,我现在无法找到'产品'表? – user1900791