0

嗨,伙计们,我正在创建一个名为“app”的应用程序。到目前为止,有一个脚手架“联系人”,只需添加一个模型调用“地址”(地址有街道、城市、地区、邮政编码、国家 5 个属性),一个联系人可以有一个或多个地址。所以这是我的工作。路线.rb

 App::Application.routes.draw do
  resources :contacts
  resources :taggings
  resources :addresses
  root :to => 'Contacts#index'
end

型号/联系人.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(&region).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

模型/标记.rb

class Tagging < ActiveRecord::Base
  attr_accessible :Address_id, :Contact_id
  belongs_to :contact
  belongs_to :address
end

型号/地址.rb

class Address < ActiveRecord::Base
  attr_accessible :City, :Country, :Region, :Street, :Zipcode
  has_many :tagging, :dependent => :destroy
  has_many :contact, :through => :taggings
end

view/contact/_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)>'

有人可以帮我检查问题出在哪里,我不是一个好人:'(...谢谢

4

1 回答 1

2

您在第 2 行有一个额外的逗号contact.rb- 第一条错误消息告诉您这一点(当它到达第 3 行时发现):

/media/sf_VM_working/app/app/models/contact.rb:3: 语法错误,意外的 tSYMBEG,期待 keyword_do 或 '{' 或 '(' has_many :taggings, :dependent => :destroy

并通过将您的属性写为小写:email, :firstname而不是:Email, :Firstname)来节省一些悲伤

于 2013-04-11T14:21:28.943 回答