虽然 mongomapper 不支持“accepts_nested_attributes_for”,但以下适用于我的 rails 3.2.7、mongo_mapper 0.11.2、mongo 1.6.4、bson 1.6.4。经常参考“rake routes”直到路由正确,即url_for,控制器方法等。请注意email.id的隐藏字段和带有路由的按钮以创建新的电子邮件项。这仅适用于嵌入式关联中的新项目(您可以完成放置/更新)。希望这可以帮助您继续前进。
-加里
应用程序/控制器/ecards_controller.rb
class EcardsController < ApplicationController
def new
@ecard = Ecard.create
respond_to do |format|
format.html { render :template => 'ecards/show' }
format.json { render json: @ecard }
end
end
def show
@ecard = Ecard.find(params[:id])
respond_to do |format|
format.html { render :template => 'ecards/show' }
format.json { render json: @ecard }
end
end
end
应用程序/控制器/emails_controller.rb
class EmailsController < ApplicationController
def new
@ecard = Ecard.find(params[:ecard_id])
@ecard.emails << Email.new
@ecard.save
respond_to do |format|
format.html { render :template => 'ecards/show' }
format.json { render json: @ecard }
end
end
end
app/views/ecards/show.html.erb
<%= form_for(@ecard) do |f| %>
<%= f.label :family, :scope => :name %><br />
<%= f.text_field :family %><br />
<% @ecard.emails.each do |email| %>
<%= f.fields_for email, :index => email do |e| %>
<%= e.hidden_field :id, :value => email.id %>
<%= e.label :pref %>
<%= e.check_box :pref %>
<%= e.label :email %>
<%= e.text_field :email %>
<br/>
<% end %>
<% end %>
<% end %>
<%= button_to 'New Email', url_for([:new, @ecard, :email]), :method => :get %>
配置/路由.rb
resources :ecards do
resources :emails
end