So I have 2 models, listings, and houses. Houses can be made independently, while all listings must be associated with a house (this a an apartment-finding site). I am trying to build a form for a new listing which has a column alongside of houses already in the database. If the user sees their house, they click it, and the form auto-fills with the house's information. I want the user to be able to change parameters (such as number of bedrooms) just in case the already-existing house has incorrect information. In this case, the user then fills out the rest of the form for the listing (availability and such), and upon submitting, it should create the new listing, and also update the existing house with the new information.
Additionally, if the user doesn't see the house in the list, the information they enter for the form should create a new house.
My progress so far: I have successfully built a nested form which always creates a new listing and a new house. Each listing "has_many" houses (the only way I could get it to work), and the houses "belong_to" a listing. Also, I have this line in the listings model:
accepts_nested_attributes_for :houses
Here is what the actual nested form looks like:
<% form_for @listing do |l| %>
<%= l.error_messages %>
...
<% l.fields_for :houses do |h| %>
...
<%end%><%end%><%end%>
And lastly, the controllers. In the "new" action I have:
@listing = Listing.new
@listing.houses.build
And my create action looks like this:
@listing = Listing.new(params[:listing])
respond_to do |format|
if @listing.save
...
This saves the new listing, and a new house. Cool stuff, but I have had no luck getting the house to update if necessary (since I know when the user has clicked an existing house, I could pass this through a variable). I have tried using different kinds of nested forms, but nothing that will save the new data. Any help is appreciated.
Thanks so much in advance!