1

I have 3 models:

class Day < ApplicationRecord
  belongs_to :goal
  has_many :day_salesmen, dependent: :destroy
  has_many :salesmen, through: :day_salesman
  validates_presence_of :date_day, :goal_id

  accepts_nested_attributes_for :day_salesmen
end

class DaySalesman < ApplicationRecord
  belongs_to :day
  belongs_to :salesman

  accepts_nested_attributes_for :salesman

end

class Salesman < ApplicationRecord
  belongs_to :company
  has_many :goal_salesmen, dependent: :destroy
  has_many :goals, through: :goal_salesmen

  has_many :day_salesmen, dependent: :destroy
  has_many :days, through: :day_salesmen

end

I want while editing Days to add salesmen in my table and associate days and salesmen through the day_salesman table. I'm trying to do this but I'm not getting it right, I'm sending my controllers and my views:

   class DaysController < ApplicationController
  before_action :find_day, only: [:show, :edit, :update]

  def index
    @day = current_owner.companies.find(params[:company_id]).goal.find(params[:goal_id]).days
  end

  def show
  end

  def edit
    @dayup = Day.new
    @day_salesmen = @dayup.day_salesmen.build
    @salesman = @day_salesmen.build_salesman
  end

  def update
    if @day.update(params_day)
      flash[:notice] = "Day updated!"
      redirect_to company_salesman_path(:id => @day.id)
    else
      flash.now[:error] = "Could not update day!"
      render :edit
    end
  end

  private

  def find_day
    @day = Day.find(params[:id])
  end

  def params_day
    params.require(:day).permit(:value, day_salesman_attributes: [:id, salesman_attributes:[:name]]).merge(goal_id: params[:goal_id])
  end
end

Salesman controller:

class SalesmenController < ApplicationController
  before_action :find_salesman, only: [:edit, :update, :destroy, :show]

  def index
    @salesman = current_owner.companies.find(params[:company_id]).salesman
  end

  def new

  end

  def show

  end

  def create
    @salesman = Salesman.new(params_salesman)
    if @salesman.save
      flash[:notice] = "Salesman saved!"
    else
      flash.now[:error] = "Cannot create salesman!"
      render :new
    end
  end

  def edit
  end

  def update
    if @salesman.update(params_salesman)
      flash[:notice] = "salesman updated!"
    else
      flash.now[:error] = "Could not update salesman!"
      render :edit
    end
  end

  def destroy
    @salesman.destroy
  end

  private

  def find_salesman
    @salesman = Salesman.find(params[:id])
  end

  def params_salesman
    params.require(:salesman).permit(:name).merge(company_id: params[:company_id])
  end
end

Days edit view:

 <%= form_for(@dayup, url: company_salesmen_path) do |f| %>
  <%= f.label :value_of_day %>
  <%= f.number_field :value %>
  <%= f.fields_for :day_salesman do |ff| %>
    <%= f.fields_for :salesman do |fff| %>
      <%= fff.label :names_of_salesmen %>
      <%= fff.text_field :name %>
    <% end %>
  <% end %>
  <%= f.submit "Create" %>
<% end %>

When I fill out the edit form it gives the following error:

Log of error:

Started POST "/companies/3/salesmen" for 172.24.0.1 at 2017-10-31 11:51:47 +0000
Cannot render console from 172.24.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SalesmenController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"xQgslyRRkHIEab/E+vEQDYJhxg28IoBNNqV7N8fJxT5oI68Tm4xHrPqjDLNX1aUb8kUVVNceaA0rtfRGzlUskQ==", "day"=>{"value"=>"500", "salesman"=>{"name"=>"Denis"}}, "commit"=>"Create", "company_id"=>"3"}
  [1m[36mOwner Load (0.5ms)[0m  [1m[34mSELECT  "owners".* FROM "owners" WHERE "owners"."id" = $1 ORDER BY "owners"."id" ASC LIMIT $2[0m  [["id", 1], ["LIMIT", 1]]
Completed 400 Bad Request in 28ms (ActiveRecord: 0.5ms)

I do not know if I'm doing it right, probably not, remembering that I want to add the salesman when I edit the day and not when I'm creating the day, I do not know if that's possible. Could anyone help?

Denis Policarpo
  • 153
  • 2
  • 12
  • I believe you want to create a nested form http://guides.rubyonrails.org/form_helpers.html#building-complex-forms – Fabrizio Bertoglio Oct 31 '17 at 09:29
  • yes I need a nested but with has many through... – Denis Policarpo Oct 31 '17 at 10:19
  • the problem is that you do not include any info from your server log in this post, the first thing to show is why is it not working? what are the errors alert in your server log? if they are not showing up, please go get them.. then if there is no error, how are your params actually showing up? go check your params and your objects... – Fabrizio Bertoglio Oct 31 '17 at 10:33

1 Answers1

0

usually in this case I check my server logs for either warning or errors.

The logic is:

  1. an object is instantiated

    @day = Day.new input_params
    
  2. an object is validated before saving

    @day.valid?
    
  3. the object is saved

    @day.save
    

if the object is not saved it is because:

you have problems with your params

@day = Day.new input_params

so your input_params are missing something or they have the wrong format, go check those params by stopping before saving the code with a binding.pry and compare the value of params with input_params

this is an example of how the input_params with nested form should be passed

{
  'person' => {
    'name' => 'John Doe',
    'addresses_attributes' => {
      '0' => {
        'kind' => 'Home',
        'street' => '221b Baker Street'
      },
      '1' => {
        'kind' => 'Office',
        'street' => '31 Spooner Street'
      }
    }
  }
}

you are missing an object

for example, you are saving an object that has a belongs_to relationship with another object, but that object does not exist... this cases

some other reason not so difficult to figure out

Fabrizio Bertoglio
  • 5,890
  • 4
  • 16
  • 57