ruby on rails - Need Help with update_attributes -
i hoping @ , give advice on how can make action better. works how it, seems unnecessary create 2 model instances.
i felt had because, though update_attributes won't update database if validation fails, still wanted have @user instance available show original @user.photo in view. i'm using paperclip.
thanks in advance.
def edit_avatar @user = user.find(@username_id) if params['post_data'].present? @user.update_attributes(params[:post_data]) @errors = @user.errors if @user.errors.count == 0 @start_jcrop = true else @user = user.find(@username_id) end end end
instead of reloading model, can use dirty changes feature. or can use
@user.reload
instead of creating new instance.
@user = user.find(@username_id) ... else @user.reload end end
you can simplify view follows
@user = user.find(@username_id) if params['post_data'].present? if @user.update_attributes(params[:post_data]) @start_jcrop = true else @errors = @user.errors @user.reload end end
and if skip reload
@user = user.find(@username_id) if params['post_data'].present? if @user.update_attributes(params[:post_data]) @start_jcrop = true else # render # previous image available @ # @user.attribute_name_was end end
Comments
Post a Comment