我在rails3中使用mongodb和mongoid。我是这一切的新手。我的模型如下图所示。
class Californium
include Mongoid::Document
field :first_name
field :License_type
Field :Licese_expiry_date
embeds_one :address
end
class Address
include Mongoid::Document
field :street
field :city
field :state
field :zip
embedded_in :Californium, :inverse_of => :address
end
我的控制器
class CaliforniaController < ApplicationController
def index
@california = Californium.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @california }
end
end
def show
@californium = Californium.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @californium }
end
end
# Here is where I have problem. I am not able to show a
# form with californium address details. My form view is show after the controller
def new
@californium = Californium.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @californium }
end
end
def edit
@californium = Californium.find(params[:id])
end
end
我的表格
<%= form_for(@californium) do |f| %>
<div class="field">
<%= f.label :license_number %><br />
<%= f.text_field :license_number %>
</div>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :license_type %><br />
<%= f.text_field :license_type %>
</div>
<div class="field">
<%= f.label :license_status %><br />
<%= f.text_field :license_status %>
</div>
<div class="field">
<%= f.label :license_expire_date %><br />
<%= f.text_field :license_expire_date %>
</div>
<div class="field">
<%= f.label :license_issue_date %><br />
<%= f.text_field :license_issue_date %>
</div>
<div class="field">
# Here I am not able to access :address.street and :address.city as it is an other
# model embedded in californium
<%= f.label :address.street %><br />
<%= f.text_field :address.street %>
</div>
<div class="field">
<%= f.label :address.city %><br />
<%= f.text_field :address.city %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我正在尝试构建一个表格,可以在其中编辑 californium 的所有细节。我无法访问 californium 的地址详细信息,因为它是 californium 集合的子集合。我能够显示 californium 的所有详细信息,包括地址,但不知道如何创建可编辑的表单。