0

I'm creating a portfolio thanks to 2 scaffolds, one for the portfolio itself and the other for the images, uploaded by carrierwave

This isn't the first time I'm doing this but this is the first time I encounter this issue:

def create
    @multifichier = Multifichier.new(multifichier_params)

    respond_to do |format|
      if @multifichier.save
        if params[:fichiers]
          params[:fichiers].each { |f|
            @multifichier.fichiers.create(titre: f)
            # tried with some random strings, no difference
          }

          # abort @multifichier.fichiers.inspect
          end
        format.html { redirect_to @multifichier, notice: 'Multifichier was successfully created.' }
        format.json { render :show, status: :created, location: @multifichier }
      else
        format.html { render :new }
        format.json { render json: @multifichier.errors, status: :unprocessable_entity }
      end
    end
  end

If I inspect @multifichier.fichiers once in the loop, I'll get the following <ActiveRecord::Associations::CollectionProxy [#<Fichier id: 34, titre: nil, created_at: "2017-05-03 23:38:34", updated_at: "2017-05-03 23:38:34", multifichier_id: 3>. As you can see, titre gives 'nil', but multifichier_id gets an attribute, and I can't understand why.

Here are my models:

fichier.rb class Fichier < ApplicationRecord belongs_to :multifile, optional: true #I want to upload images one by one too mount_uploader :titre, FichierUploader end

multifichier.rb

class Multifichier < ApplicationRecord
  has_many :fichiers
  accepts_nested_attributes_for :fichiers
end

What am I missing? This is also the first time I'm using a scaffold with JSON but I don't believe this is linked.

Thank you in advance, please ask if you need to see something else!

edit 1

I realized while reading your answers that I didn't show you how I set up my params. I also created another scaffold with one parameter, but I still can't make it work

def multifichier_params
  params.require(:multifichier).permit(:titre, :fichiers => [:titre])
end

Once again, I don't get why this doesn't work when I have 3 other projects on which it worked pefectly

Jaeger
  • 1,686
  • 1
  • 30
  • 50
  • You probably need to whitelist your params in the controller, see this answer http://stackoverflow.com/questions/27682951/rails-4-nested-form-with-accepts-nested-attributes-for-controller-setup – Rafal May 04 '17 at 00:21
  • do you have `fichiers_attributes` inside your `multifichier_params`? you have to permit the nested attributes – amrrbakry May 04 '17 at 00:26
  • Does it accept if you change the params to `params.require(:multifichier).permit(:titre, fichiers_attributes: [:titre])`? – oneWorkingHeadphone May 04 '17 at 12:47
  • No, it doesn't.. And I don't get why the "fichier_id" column gets successfully modified but `:titre` won't. – Jaeger May 04 '17 at 13:00

0 Answers0