I have just started learning Ruby on Rails from pragmatic programmers 'Agile Web Development with Rails'.
I have wrote a small application and when it runs I receive the following syntax error when I run it:
/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)>'
The code for the DB is as follows:
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
Versions are as follow: ruby 1.9.3p362 Rails 3.2.11
Should I be checking my sqlite installation and how so?
You just need to fix this line:
t.string : title
to:
t.string :title
Your model also has the same problem in the attr_accessible
call.
:something
are actually symbols in Ruby. You cannot leave any space between :
and the symbol name.
Change t.string : title
to t.string :title
in the migration file.
And in your product model,
Change attr_accessible : title
to attr_accessible :title
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.