SyntaxError嗨,夥計們,我創建一個應用程序調用「應用程序」。到目前爲止,只有一個腳手架'聯繫人',只需添加一個模型電話'地址'(地址有街道,城市,地區,郵政編碼,國家5屬性),一個聯繫人可以有一個或多個地址。所以這是我的工作。 的routes.rbContactsController#new
App::Application.routes.draw do
resources :contacts
resources :taggings
resources :addresses
root :to => 'Contacts#index'
end
模型/ contact.rb
class Contact < ActiveRecord::Base
attr_accessible :Email, :Firstname, :Lastname, :Mobilephone,
has_many :taggings, :dependent => :destroy
has_many :addresses, :through => :taggings
validates_presence_of :Firstname, :Lastname
attr_writer :address_street, :address_city, :address_region, :address_zipcode, :address_country
after_save :assign_street, :assign_city, :assign_region, :assign_zipcode, :assign_country
def address_streets
@address_streets || addresses.map(&street).join('')
end
def address_citys
@address_citys || addresses.map(&city).join('')
end
def address_regions
@address_regions || addresses.map(®ion).join('')
end
def address_zipcode
@address_zipcodes || addresses.map(&zipcode).join('')
end
def address_countrys
@address_countrys || addresses.map(&country).join('')
end
def assign_streets
if @assign_streets
self.addresses = @assign_streets.split(/\s+/).map do |street|
Address.find_or_create_by_street(street)
end
end
end
def assign_citys
if @assign_citys
self.addresses = @assign_citys.split(/\s+/).map do |city|
Address.find_or_create_by_city(city)
end
end
end
def assign_regions
if @assign_regions
self.addresses = @assign_regions.split(/\s+/).map do |region|
Address.find_or_create_by_region(region)
end
end
end
def assign_zipcodes
if @assign_zipcodes
self.addresses = @assign_zipcodes.split(/\s+/).map do |zipcode|
Address.find_or_create_by_zipcode(zipcode)
end
end
end
def assign_countrys
if @assign_countrys
self.addresses = @assign_countrys.split(/\s+/).map do |country|
Address.find_or_create_by_country(country)
end
end
end
end
模型/ tagging.rb
class Tagging < ActiveRecord::Base
attr_accessible :Address_id, :Contact_id
belongs_to :contact
belongs_to :address
end
模型/ address.rb
class Address < ActiveRecord::Base
attr_accessible :City, :Country, :Region, :Street, :Zipcode
has_many :tagging, :dependent => :destroy
has_many :contact, :through => :taggings
end
視圖/接觸/_form.html.erb 停止工作,因爲我在下面添加!!代碼:
<form>
<p>
<%= f.label :street %><br />
<%= f.text_field :address_street %>
</p>
</form>
而且contact_controllor.rb我沒有碰過任何東西。當我開始我的服務器,並嘗試加載到「創建新的聯繫人」軌告訴我
SyntaxError in ContactsController#new
/media/sf_VM_working/app/app/models/contact.rb:3: syntax error, unexpected tSYMBEG, expecting keyword_do or '{' or '('
has_many :taggings, :dependent => :destroy
^
/media/sf_VM_working/app/app/models/contact.rb:33: syntax error, unexpected keyword_do_block, expecting keyword_end
self.addresses = @assign_streets.split(/\s+/).map do |street|
^
/media/sf_VM_working/app/app/models/contact.rb:41: syntax error, unexpected keyword_do_block, expecting keyword_end
self.addresses = @assign_citys.split(/\s+/).map do |city|
^
/media/sf_VM_working/app/app/models/contact.rb:45: syntax error, unexpected keyword_end, expecting $end
和
app/controllers/contacts_controller.rb:1:in `<top (required)>'
可能有人幫我檢查哪裏出了問題,我不是一個好:」 (...謝謝
保存自己的一些悲痛非常感謝你。當我創建我使用大寫的模型時,那麼可以使用小寫嗎? – 2013-04-11 14:30:00
除非您在某些時候修改了排序規則,否則數據庫通常適用於此(不區分大小寫)。一般來說,如果你堅持使用規範 – PinnyM 2013-04-11 14:35:45
,那麼你會更容易使用Rails。非常感謝。現在機器告訴我......未定義的局部變量或方法'街道' – 2013-04-11 14:48:36