1

There are existing companies and orientations(landscape, portrait) and have to create app_accesses(STI type: iPhone, iPad, desktop) for the company. The view is drop-down of app_accesses types and checkbox of orientations and the requirement is to save app_accesses for the company with the type selected and orientations checked

So for single company's single AppAccess type and multiple orientation_id's the join table should be saved

Models

  class Company < ApplicationRecord
    has_many :app_accesses
    has_many :orientations, through: :app_accesses
    accepts_nested_attributes_for :app_accesses
  end

  class Orientation < ApplicationRecord
    has_many :app_accesses
    has_many :companies, through: :app_accesses
  end

  class AppAccess < ApplicationRecord
    belongs_to :company
    belongs_to :orientation
    accepts_nested_attributes_for :orientation
  end

And Controller looks like

class CompaniesController < ApplicationController
  before_action :find_company, only: [:show, :edit, :update, a_access_new, :a_access_create]
  def new
    @company = Company.new
  end

  def create
    @company = Company.new(company_params)
    @company.save  
  end

  def a_access_new
    @com_acc = Company.find(params[:id])
    @com_acc.app_accesses.build.build_orientation
  end

  def a_access_create
    //save attributes here    
  end
end

The view looks like

= form_for @com_acc, url: a_access_create_company_path(@com_acc), :method => :POST , local: true do |f|
  = f.fields_for :app_accesses do |app|
    .row
      - devices = ['Iphone', 'Ipad', 'Desktop']
      = app.label :type, 'Device Type:', class: "col-lg-3 control-label"
      .col-lg-9
        = app.collection_select :type, devices, :to_s, :to_s, class: 'form-control custom-checkbox'
      = app.fields_for :orientation do |orient|
        = orient.collection_check_boxes(:id, Orientation.all, :id, :name, include_hidden: false) do |b|
          = b.check_box
          = b.label
    .row
      = f.submit 'Save'

How would the strong parameters be? I am facing issues with the parameters and they are not being saved.

Also getting an error like accepts_nested_attributes_for to link to existing record, not create a new one while saving

AkashP
  • 676
  • 6
  • 14

0 Answers0