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

4
  • You probably need to whitelist your params in the controller, see this answer stackoverflow.com/questions/27682951/… Commented May 4, 2017 at 0:21
  • do you have fichiers_attributes inside your multifichier_params? you have to permit the nested attributes Commented May 4, 2017 at 0:26
  • Does it accept if you change the params to params.require(:multifichier).permit(:titre, fichiers_attributes: [:titre])? Commented May 4, 2017 at 12:47
  • No, it doesn't.. And I don't get why the "fichier_id" column gets successfully modified but :titre won't. Commented May 4, 2017 at 13:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.