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

  • SEARCH
  • Home
  • 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 8534461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:12:08+00:00 2026-06-11T10:12:08+00:00

I’m using rails 3.2 and I building a nested form. But things are not

  • 0

I’m using rails 3.2 and I building a nested form. But things are not working as I expect. First of all, my model is a Company with has many Addresses. Here is the model

class Company
    include Mongoid::Document
    include Mongoid::Timestamps

    field :name,                        :type => String
    field :description,         :type => String
    field :order_minimun,       :type => Float

    belongs_to :user

    has_many :addresses

    validates_presence_of :name, :description, :order_minimun
  validates_length_of :name, minimum:2, maximum: 30
  validates_length_of :description, minimum:5, maximum: 140
    validates :order_minimun, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }

    accepts_nested_attributes_for :addresses
    validates_associated :addresses

end

class Address
    include Mongoid::Document
    include Mongoid::Timestamps
    include Mongoid::Spacial::Document


    field :street,      :type => String
    field :number,      :type => Integer

    field :phone,           :type => String

    field :location,        :type => Array,     spacial: {lat: :latitude, lng: :longitude, return_array: true }

    embeds_many :delivery_zones


    belongs_to :company
    belongs_to :city

    has_many :openingTimeRange

    validates_presence_of :street, :number
  validates_length_of :street, minimum:1, maximum: 30
    validates_length_of :number, minimum:1, maximum: 6
    validates_length_of :phone, minimum:5, maximum: 60


    attr_accessible :street, :number, :company_id, :city_id, :location, :phone, :delivery_zones, :latitude, :longitude

end

As you can see the Company model has:

accepts_nested_attributes_for :addresses
validates_associated :addresses

So, I think a can build a nested form. Here is the code of the form

<%= form_for [:admin,@company],:url =>admin_company_path(@company), :html => {:class => "form-horizontal"}  do |f|%>

    <legend><%= t '.legend' %></legend>

    <%= group_input_field f, :name%>

    <%= group_field_for f, :description do%>
        <%= f.text_area :description, :rows => 5%>
    <% end -%>

    <%= group_input_field f, :order_minimun%>

    <%= f.fields_for :addresses do |builder|%>
        <%= render 'address_fields', :f=> builder%>
    <% end %>

    <div class="form-actions">
        <%= f.submit :class => 'btn btn-primary btn-large', :disable_with => t('button.saving') %>
        <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                admin_companies_path, :class => 'btn btn-large btn-danger' %>
  </div>

<% end %>

the _address_fields.html.erb

<%= group_input_field f, :street%>
<%= group_input_field f, :number%>

I have a simple helper to generate the form fields with bootstrap

def group_input_field(f,field, options={})
    has_error = f.object.errors.has_key? field
    klass = has_error ? "control-group error": "control-group"
    content_tag(:div, :class => klass) do
        f.label(field, :class => 'control-label')+
        content_tag(:div, :class => 'controls') do
            f.text_field(field, :class => 'input')+
            show_error(f.object,field,has_error)+
            show_help(options)
            end
        end
end

Finaly the controller:

class Admin::CompaniesController < ApplicationController

    def new
        #crea una nueva compañia
        @company = Company.new
    end

    def edit
        @company = Company.find params[:id]
    end

    def create
        @company = Company.new(params[:company])
        if @company.save
            redirect_to :action => 'index'
        else
            render 'new'
        end
    end

    def update
        @company = Company.find(params[:id])
        if @company.update_attributes(params[:company])
            redirect_to :action => 'index'
        else
            render 'edit'
        end
    end

end

What is happening a couple of things. First, I have a company with two addresses, and I am able to edit the first one, any change on the second one is not persisted. Then the addresses fields are not validated (if i leave everything in blank when i open the form again the addresses were not save and I can see the original values). And when edit a field in any address, and any field value of the company is not valid, after the form is submited i can see the errors on the company model, but the address are displayed with the original values, so the edited values were lost.

Hope be clear.

Thanks in advance.

  • 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-11T10:12:10+00:00Added an answer on June 11, 2026 at 10:12 am

    Well, I’ve found the answer. I was using the version of mongoid 3.0.4. I run the command bundle update mongoid and mongoid was updated to the version 3.0.6. And the problems were fixed.

    Thanks. Hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.