I have seen the other links to this similar question but none of them seem to work in my case.
My update action is creating new records in mysql database
My app consists of 3 models
FashionModel
ModelProfile
andMeasurement
They are defined as follows:
class FashionModel < ActiveRecord::Base
has_secure_password
has_one :model_profile
has_one :measurement
accepts_nested_attributes_for :model_profile
accepts_nested_attributes_for :measurement
end
class ModelProfile < ActiveRecord::Base
belongs_to :fashion_model
end
class Measurement < ActiveRecord::Base
belongs_to :fashion_model
end
The fashion_model_controller.rb
is as follows:
class FashionModelsController < ApplicationController
def new
@fashion_model = FashionModel.new
end
def create
@fashion_model = FashionModel.new(fashion_models_sign_up_params)
if @fashion_model.save
flash[:success] = "Welcome to Meriad"
session[:fashion_model_id] = @fashion_model.id
redirect_to edit_fashion_model_path(@fashion_model)
else
render 'new'
end
end
def edit
@fashion_model = FashionModel.find(params[:id])
@fashion_model.build_model_profile
@fashion_model.build_measurement
end
def update
@fashion_model = FashionModel.find(params[:id])
if @fashion_model.update(fashion_models_edit_params)
flash[:success] = "Saved!"
redirect_to fashion_model_path(@fashion_model)
else
render 'edit'
end
end
def show
@fashion_model = FashionModel.find(params[:id])
end
end
The fashion_model_edit_params
are
def fashion_models_edit_params
params.require(:fashion_model).permit(:first_name,
:last_name,
:email,
:password,
model_profile_attributes: [:id,
:location,
:bio, :gender,
:phone_number,
:rate,
:profile_image,
:birthdate],
measurement_attributes: [:id,
:feet,
:inches,
:bust,
:waist,
:hips,
:dress,
:shoes,
:hair,
:eyes])
end
I want something on these lines:
The fashion model signs up to the app through
new.html.erb
(which is stored in thefashion_models
table)The
index.html.erb
contains a list of allfashion_models
with an option to edit information (update their profile)The
edit.html.erb
contains fields from themodel_profiles
table as well as themeasurements
table, both of which have the foreign keys to thefashion_models
table.
My fashion_models/new.html.erb
template is pretty straightforward containing first_name
, last_name
, email
, password
.
My fashion_models/edit.html.erb
template is something like:
<%= form_for @fashion_model do |f| %>
# fashion_model fields here
<%= f.fields_for :model_profile do |t| %>
# model_profile fields here
<% end %>
<%= f.fields_for :measurement do |t| %>
# measurement fields here
<% end %>
<% end %>
Now, whenever I edit a fashion_model/:id
, the model_profile
and measurement
create a new record in the database rather than updating the existing record. Also, when I am on the Edit Profile
page, none of the fields pre populate with the existing data. I have to manually enter all the data again.
First, I thought it was because of the build
methods, but when I remove them, the fields_for
do not display.
Appreciate any help!