我有两个模型一个地址模型
class Address < ActiveRecord::Base
attr_accessible :address1, :address2, :country, :county, :customer_id, :town
belongs_to :customer
end
和客户模型
class Customer < ActiveRecord::Base
attr_accessible :email, :firstname, :lastname, :password, :phone
has_one :address
end
我有使用脚手架生成的 CRUD 功能。我想要做的是当我添加一个新客户时,我可以从文件中的地址中为客户选择一个地址,也可以不选择地址。我可以使用它查看客户及其地址。
def showaddress
@customer = Customer.find(params[:id])
@address = @customer.address
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @customer }
end
end
这是我当前用于创建新客户的控制器
def new
@customer = Customer.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @customer }
end
end
所以基本上我想要做的是当我创建一个新客户时,有一个地址的下拉列表,用户可以点击它来将地址与客户相关联