0

Hello everybody I'm trying to make an app with a user and children with has_many and belongs_to association.

When I try to modify children controller I have this error : undefined method `build'. This is my controller :

# GET /enfants/new
  def new
    @enfant = current_user.enfants.build
  end

  # GET /enfants/1/edit
  def edit
  end

  # POST /enfants
  # POST /enfants.json
  def create
    @enfant = current_user.build(enfant_params)

    respond_to do |format|
  if @enfant.save
    format.html { redirect_to @enfant, notice: 'Enfant was successfully created.' }
    format.json { render :show, status: :created, location: @enfant }
  else
    format.html { render :new }
    format.json { render json: @enfant.errors, status: :unprocessable_entity }
  end
 end
end

I tried some of answers in this website but there is no solution to my problem. Can you help me please ? Thank you.

1 Answer 1

1

change current_user.build(enfant_params) in create action to current_user.enfants.new(enfant_params)

Sign up to request clarification or add additional context in comments.

Comments

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.