Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7985205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:29:38+00:00 2026-06-04T11:29:38+00:00

I am using mongodb with mongoid in rails3. I am newbie to all this.

  • 0

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

My Controller

 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  

My form

<%= 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 %>

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:29:39+00:00Added an answer on June 4, 2026 at 11:29 am

    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

    <%= 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

      match 'california/:id' => 'california#edit', via: :get
      match 'california/:id' => 'california#update', via: :post
    

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that looks like this: class SearchService include Mongoid::Document key :name,
I'm using MongoDB and MongoID in a rails app, how can some models be
I am currently working on a rails app where we are using mongoid/mongoDB on
I've got a Ruby on Rails app that is using MongoDB for datastorage. Mongoid::Timestamp
I'm using mongodb 2.1 and how to translate this query into php db.counter.aggregate([ {
I have recently been getting my feet wet in MongoDB using Mongoid w/ Rails
I recently played with MongoDB in Rails using Mongoid . I like the ability
I've got a Rails 3 app (using Mongodb and Mongoid if that makes a
I'm trying to build a new Rails app with MongoDB using Mongoid as the
I'm using MongoDB with Mongoid and trying to put in place a rudimentary search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.