Home accepts_nested_attributes_fields not showing for join table
Reply: 0

accepts_nested_attributes_fields not showing for join table

user8073 Published in September 20, 2018, 10:22 pm

I have the following tables

class Region < ActiveRecord::Base
  has_many :companies, through: :companies_regions
  has_many :companies_regions, :dependent => :destroy

class Company < ActiveRecord::Base
  has_many :regions, through: :companies_regions
  has_many :product_type, dependent: :destroy
  has_many :companies_regions, :dependent => :destroy

  accepts_nested_attributes_for :companies_regions, :allow_destroy => true

class CompaniesRegion < ActiveRecord::Base
  belongs_to :company
  belongs_to :region

I want to create a new company and i want to be able to add a new region accordingly to the CompaniesRegion table.


<%= simple_form_for(['admin', @company]) do |f| %>
  <%= f.error_notification %>
  <div class="form-group">
    <%= f.input :name %>
  <div class="row">
    <div class="col-md-12">
      <h4>Basic Coverages</h4>
      <div class="row form-group">
        <label class="col-md-1">#</label>
        <label class="col-md-3">Coverage</label>
        <label class="col-md-1">Description</label>
        <%= f.simple_fields_for :companies_regions do |company_region| %>
          <%= render 'company_region', f: company_region  %>
        <% end %>
        <%= link_to_add_association 'New Region', f, :companies_regions, partial: 'company_region' %>
  <div class="form-actions">
    <%= f.button :submit %>
<% end %>


<div class="nested-fields form-group row">
  <div class="col-md-1"></div>
  <div class="col-md-3">
    <%#= f.select :region_id, class: 'form-control', placeholder: 'Region' %>
    <%= f.input_field :region_id, collection: ['Asia', 'America'], class: 'form-control', prompt: 'Please Select' %>
  <div class="col-md-1">
    <%= link_to_remove_association(f, title: 'Remove') do  %>
      <span class="glyphicon glyphicon-remove"></span>
    <% end %>

The problem here is that when i click the New Region link, i am expecting it to display the details in the _company_region.html.erb but unfortunately it doesnt.

Nothing shows. It doesnt display any data. It however refreshes the page which is absurd.

Dont know if it is because my table is a join table hence the problem or if there is something else that i am missing but based on documentation, this should be fine and should work.

Any help is deeply appreciated

share|improve this question
  • 1
    Please include the _company_region partial in the question. partial: is needed because the partial does not follow the naming convention (company_region_fields) – Pablo May 5 at 1:21

2 Answers 2

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.368042 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO