I am having a Trouble to check if that param is empty or not on ruby on rails -
i trying check if user_avatar present or not form(submitted user).if select different image update need go cropping otherwise redirect edit page.
but if run bellow code on controller directly redirecting edit page if select , send image also.
controller
if @user.update(user_params) if((@user.user_avatar.present?) && (!params[:user_avatar].blank?) ) format.html { render :action => 'crop'} else format.html { redirect_to edit_user_path, notice: 'user updated.' } format.json { head :no_content } end else format.html { render action: 'edit' } format.json { render json: @user.errors, status: :unprocessable_entity }
end
def user_params params.require(:user).permit(:user, :crop_x, :crop_y, :crop_w, :crop_h, :user_avatar, :fname, :lname, :city_id, :location_id, :email, :password, :course, :phone_no) end
in rails can use has_key?
method returns true
or false
, rewriting above if condition
if @user.user_avatar.present? , params[:user].has_key?(:user_avatar) # code end
Comments
Post a Comment