使用Rails形式访问Mongoid中的子集合以编辑和创建新条目 [英] Access subcollection in mongoid with rails form to edit and create new entry
问题描述
我正在mongodb中使用mongodb在rails3中.我是这一切的新手.我的模型如下图所示.
I am using mongodb with mongoid in rails3. I am newbie to all this. My models are as shown below.
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 %>
我正在尝试创建一个表格,在该表格中可以编辑all的所有详细信息.我不能访问的地址详细信息,因为它是集合的子集合.我能够显示the的所有详细信息,包括地址,但不显示如何创建可编辑的表格.
I am trying to build a form where in all the details of the californium could be edited. I am not able to access californium's address details as it is a subcollection of californium collection. I am able to display all the details of the californium including the address but dont how to create a editable form.
推荐答案
尝试为嵌入式模型使用表单帮助程序"fields_for",如以下工作示例中所示. 希望这会有所帮助.
Try using the form helper "fields_for" for your embedded model as in the following working example. Hope that this helps.
花了一段时间才能解决错别字和不一致之处,因此,如果您以后希望更快地回答问题, 请使您的问题尽可能准确和尽可能少.
It took a while to wade through the typos and inconsistencies, so if you want a faster answer in the future, please make your question both as accurate and as minimal as possible.
class Californium
include Mongoid::Document
field :name
field :license_type
embeds_one :address
end
class Address
include Mongoid::Document
field :street
field :city
field :state
field :zip
embedded_in :california, :inverse_of => :address
end
app/views/edit.html.erb
app/views/edit.html.erb
<%= form_for :californium do |f| %>
<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>
<%= fields_for @californium.address do |af| %>
<div class="field">
<%= af.label :street %>
<br/>
<%= af.text_field :street %>
</div>
<div class="field">
<%= af.label :city %>
<br/>
<%= af.text_field :city %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
config/routes.rb
config/routes.rb
match 'california/:id' => 'california#edit', via: :get
match 'california/:id' => 'california#update', via: :post
test/functional/california_controller_test.rb
test/functional/california_controller_test.rb
require 'test_helper'
class CaliforniaControllerTest < ActionController::TestCase
def setup
Californium.delete_all
end
test "form" do
cal = Californium.create(name: 'Benjamin Spock', license_type: 'MD', address: Address.new(street: '311 Temple St', city: 'New Haven', state: 'CT', zip: '06511'))
assert_equal(1, Californium.count)
p Californium.find(cal.id)
get :edit, id: cal.id
assert(assigns(:californium))
assert_response(:success)
puts @response.body
end
end
这篇关于使用Rails形式访问Mongoid中的子集合以编辑和创建新条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!