嗨,大家好我正在创建一个应用程序调用'app'。到目前为止,有一个脚手架'联系',只需添加一个模型调用'地址'(地址有街道,城市,地区,邮政编码,国家5属性),一个联系人可以有一个或几个地址。所以这是我的工作。 的routes.rb
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
查看/ contact / _form.html.erb 停止工作,因为我在下面添加代码!!:
<form>
<p>
<%= f.label :street %><br />
<%= f.text_field :address_street %>
</p>
</form>
而且contact_controllor.rb我没有触及任何东西。当我启动我的服务器并尝试加载到“创建新联系人”时,rails告诉我
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)>'
有人可以帮我查一下问题所在,我不是一个好人:'(...谢谢
答案 0 :(得分:2)
contact.rb
的第2行有一个额外的逗号 - 第一条错误消息告诉你(当它遇到第3行时发现):
/media/sf_VM_working/app/app/models/contact.rb:3:语法错误,意外的tSYMBEG,期待keyword_do或'{'或'('' has_many:taggings,:dependent =&gt; :破坏
通过将属性编写为小写(:email, :firstname
而不是:Email, :Firstname
)来节省一些悲伤。