I am doing a multi-form wizard, following the steps provide by Ryan Bates. Creating a new record works, so I was trying to use the same logic for when I edit a record. However, the values that I change do not change — when I edit something from the first form, go forward then backwards, my edits do not save. Here is the code in my controller:
def edit
session[:edit] = "Only change the fields you wish to edit"
@demographic = Demographic.find(params[:id])
session[:demographic_params] ||= {}
end
def update
session[:demographic_params].deep_merge!(params[:demographic]) if params[:demographic]
@demographic = Demographic.find(params[:id])
@demographic.current_step = session[:demographic_step]
if params[:back_button]
@demographic.previous_step
elsif @demographic.last_step?
@demographic.update_attributes(params[:demographic])
updated = true
else
@demographic.next_step
end
session[:demographic_step] = @demographic.current_step
if not updated
render "edit"
else
session[:demographic_params] = session[:demographic_step] = nil
flash[:notice] = "Entry entered successfully"
redirect_to demographic_path
end
end
What should I change that allows for saving the edits?
I don’t know if this will work, but I think that should be something like this to save on every “step change”:
I.e., move the
@demographic.update_attributesoutsite the “step-by-step” logic.So, I think that you should walk this way to solve your problem.
Hope this helps.