0

私は2つのモデルとアドレスモデルを持っています

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

したがって、基本的に私がやりたいことは、新しい顧客を作成するときに、ユーザーがクリックして住所を顧客に関連付けることができる住所のドロップダウンリストがあることです

4

2 に答える 2

0

コントローラの新しいメソッドですべてのアドレスの変数を初期化し、ビューページでその変数を使用して、フォームの選択ボックスに入力できます。

def new
  @customer = Customer.new
  @addresses = Address.all
  #...
end

または、以下に示すように、フォームにアドレスを直接入力します

select_tag 'address_id', options_for_select(Address.all.collect{ |a| [a.name, a.id] })
于 2013-03-21T11:05:25.303 に答える
0

これを試して、

<%= select_tag :address_id, Customer.all.collect {|p| [ p.address_title, p.id ] } %>
于 2013-03-21T11:07:50.667 に答える