myzen2
myzen2

Reputation: 1

Rails - Create an object with user_id

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.

Upvotes: 0

Views: 103

Answers (1)

basiam
basiam

Reputation: 1567

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

Upvotes: 1

Related Questions