So the flow of the problem is as follow, someone has an object, that has a title. The user can edit this object’s name in its edit controller with a form. The name is required to not be empty, so if the user tries to save the object with its name empty, it throws a validation error.
if @object.update_attributes(params[:object])
flash[:notice] = "Successfully updated object"
redirect_to @object
else
render :action => "edit"
end
This validation error causes render :action => "edit" to be called. The problem arises here as on the edit view, both a series of breadcrumbs and a title are attempted to be filled, but as @object.update_attributes(params[:object]) has already been called, the object, while not being saved to the database, has now got its title = “”. This makes the breadcrumb look like “Objects >> >> Edit” instead of “Objects >> AwesomeObject >> Edit”
So my question is can i retain the last previous saved name, the one that will still be in the database to use as a placeholder instead of “” while the user fixes the blank field?
It is possible to add a method that retrieves the name again from the database, for example like this:
Object is the name of the model and the call to persisted just makes sure that the instance exists in the database. However, this might generate unnecessary database calls. Another option could be to use the built-in
changeshash to see what the previous value was. The changes hash works like this:So you should be able to retrieve the previous title with a method like this:
This will check if there is any error with the title and if it is then take the previous value of the title. But if there are no errors then it will default to the current value of title.