0
class Ecard
  include MongoMapper::Document

  key :family, String
  key :given, String
  key :additional, String
  key :prefix, String
  key :suffix, String

  has_many :emails

end

class Email
  include MongoMapper::EmbeddedDocument

  key :pref, Boolean
  key :email, String

end

in the ecards controller

 def new
    @ecard = Ecard.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @ecard }
    end
  end

and in my form

<%= form_for(@ecard) do |f| %>

<%= f.label t :family, :scope => :name  %><br />
<%= f.text_field :family %><br />

<%= @ecard.emails.each do |email| %>
    <%= f.fields_for email, :index => email do |e| %>
        <%= e.label :pref %>
        <%= e.check_box :pref %>
        <%= e.label :email %>
        <%= e.text_field :email %>
    <% end %>
  <% end %>
 <% end %>

how to create à new email nested resources ?

1 Answer 1

1

While mongomapper does not support "accepts_nested_attributes_for", the follwing works for me with rails 3.2.7, mongo_mapper 0.11.2, mongo 1.6.4, bson 1.6.4. Refer often to "rake routes" until the routing is correct, i.e., url_for, controller methods, etc. Please note the hidden field for email.id and the button with the route to create a new email item. This is just for a new item in an embedded association (you get to complete the put/update). Hope that this helps you move forward.

-Gary

app/controllers/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

app/controllers/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 %>

config/routes.rb

resources :ecards do
    resources :emails
end
Sign up to request clarification or add additional context in comments.

1 Comment

thank Gary, i found this solution, see my answer, but i have another problem, with check_box. I forgot, in a second time, i think use jquery to additional email when i create or edit a ecard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.