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