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 3844984
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:10:27+00:00 2026-05-19T16:10:27+00:00

I have two separate models/controllers. One is called Merchants another call CompanyLocations. I need

  • 0

I have two separate models/controllers. One is called “Merchants” another call “CompanyLocations”. I need to be able to add a new merchant and at least one location on the same form. With button that allow me to add more locations if needed.

Using sample data I have populated the first 5 of 30 merchants with location data. And now have the Show Merchant view working fine, it shows the Merchant and the company locations underneath.

My problem is on thew new/edit pages. I cannot seem to get another location put into the DB associated with the Merchant id.

The error I am getting is : “undefined method `company_locations’ for nil:NilClass” after i try to add a company location on the merchant edit page.

As well i get an error when i try to add a new merchant. And the add button shows up twice, once for the new merchant and once for the add location.

In my models I have
merchant.rb

has_many :company_locations, :dependent => :destroy

company_location.rb

belongs_to :merchant

Here are my files:
controllers/merchant_controller.rb

  def new
    @merchant = Merchant.new
    @company_location = @merchant.company_locations.new
  end

  def edit
    @merchant = Merchant.find(params[:id])
    @company_location = @merchant.company_locations.new
  end

controllers/company_locations_controller.rb

  def create
    @company_location = @merchant.company_locations.build(params[:company_location])
    if @company_location.save
      redirect_to 'merchants/show', :flash => { :success => "Company Location Added" }
    else
      redirect_to 'merchants/index', :flash => { :error => "Location not saved" }
    end
  end
  def update

  end

views/merchants/new.html.erb

<h1>New merchant</h1>

<%= render 'form' %>

<%= render 'company_locations/form' %>

<%= link_to 'Cancel', merchants_path %>

views/merchants/_form.html

<%= form_for(@merchant) do |f| %>
  <% if @merchant.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@merchant.errors.count, "error") %> prohibited this merchant from being saved:</h2>

      <ul>
      <% @merchant.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :company_name %><br />
    <%= f.text_field :company_name %>
  </div>
  <div class="field">
    <%= f.label :fanpage_url %><br />
    <%= f.text_field :fanpage_url %>
  </div>
  <div class="field">
    <%= f.label :twitter_id %><br />
    <%= f.text_field :twitter_id %>
  </div>
  <div class="field">
    <%= f.label :website_url %><br />
    <%= f.text_field :website_url %>
  </div>
  <div class="field">
    <%= f.label :contact_email %><br />
    <%= f.text_field :contact_email %>
  </div>
  <div class="field">
    <%= f.label :active %><br />
    <%= f.check_box :active %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

views/company_locations/_form.html.erb

<%= form_for @company_location do |f| %>
    <%= render 'shared/error_messages', :object => f.object  %>
    <div class="field">
    <%= f.label :address1 %><br />
    <%= f.text_field :address1 %>
  </div>
    <div class="field">
    <%= f.label :address2 %><br />
    <%= f.text_field :address2 %>
  </div>
    <div class="field">
    <%= f.label :city %><br />
    <%= f.text_field :city %>
  </div>
    <div class="field">
    <%= f.label :state %><br />
    <%= f.text_field :state %>
  </div>
    <div class="field">
    <%= f.label :zip %><br />
    <%= f.text_field :zip %>
  </div>
    <div class="field">
    <%= f.label :phone %><br />
    <%= f.text_field :phone %>
  </div>
    <div class="field">
    <%= f.label :fax %><br />
    <%= f.text_field :fax %>
  </div>
    <div class="actions">
        <%= f.submit "Add Location" %>
    </div>
<% end %>
  • 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-05-19T16:10:28+00:00Added an answer on May 19, 2026 at 4:10 pm

    If I understand your example correctly, I think the basic problem is that you are creating two separate forms on your view for two separate models. The beauty of Rails (and most current frameworks) is the ability to use ‘nested forms’. There is a nice overview (though a bit outdated) here:

    http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

    And you can see a great Railscast on the topic here:

    http://railscasts.com/episodes/196-nested-model-form-part-1

    Generally speaking, you should be able to create one form that can edit the Merchant as well as its associated CompanyLocations in one fell swoop. It might take a bit of time to wrap your head around, but it simplifies your controllers and future development a great deal. Is that helpful?

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

Sidebar

Related Questions

We have two databases, in two separate locations. One of the databases resides in
I have an odd need to open two separate instances of excel and having
I'm setting up a directory application for which I need to have two separate
I need to have two separate pages on the site I'm planning to build
I have two models generated with generate scaffolding, one is a LogBook the other
I have two models with a one-to-one association. class User < ActiveRecord::Base has_one :setting
I want to have two separate forms on a single create page and one
I have a form that calls on two separate models. My validation works correctly
We have two separate web-apps, say 'retailUI' and 'bulkUI'. These two are basically two
I have two divs and two separate links that triggers slideDown and slideUp for

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.