Prezes Łukasz
Prezes Łukasz

Reputation: 968

Getting value of object id from new method to create

I have problem with getting value of found id from new method to create.

New method:

def new
  @vacation = Vacation.new
  @vacation.person = @person
end

Result of @person:

#<id: 1, first_name: ... >

@vacation.person is also good. For now all is well. But after this, when I fill form and click submit:

def create
  @vacation = Vacation.new(vacation_params)
end

Result:

#<Vacation id: ..., person_id: nil >

But now @person is nil and also @vacation.person is nil. I don't have idea how to send value of id to create method.

Method to find id.

before_action :set_person

private

def set_person
  @person = Person.find(params[:id]) unless params[:id].blank?
end

vacation_params:

def vacation_params
  params.require(:vacation).permit(:start_at, :end_at, :free, :reason, :person_id, :accepted)
end

Upvotes: 2

Views: 183

Answers (1)

Prezes Łukasz
Prezes Łukasz

Reputation: 968

I resolved my problem, by adding hidden field.

= f.input :person_id, as: :hidden, input_html: { value: @person.id}

Upvotes: 1

Related Questions