N
N
Nickle2015-03-05 02:35:49
Ruby on Rails
Nickle, 2015-03-05 02:35:49

How to solve a problem with a method in multi-model forms?

There is a house controller in which standard methods are specified. I added a custom apartment method to the controller.
The house and apartment models have a has_one and belongs_to relationship, respectively.
In the view, I make a nested form.

= form_for @house do |f|
    = f.fields_for :apartment do |ff|
        ...

In the controller
def apartment
     @house.build_apartment
end

For obvious reasons, when saving, I create a record, but when I call the method again, the record is overwritten. How can I make it create on first save and update on change?
PS I'm new to RoR.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
4
4itosik, 2015-03-05
@4itosik

so maybe you don't need to use a separate method in the controller to create the apartment.
and in house_params add apartment_attributes: [:id, :_destroy] (complete with your Apartment model attributes), for the has_many connection it works great.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question