ruby on rails - How can I force a redirect_to to redirect only after Active Record updates the database? -
ruby on rails - How can I force a redirect_to to redirect only after Active Record updates the database? -
when update user in rails app , update record, have press submit button twice redirect page want. first time submit button, page seems redirect edit page again, flash message not show up. when records full, supposed redirect show page, after sec click. have @asset = @user.asset.update_attributes()
before in controller action.
below code:
def update @user.update(user_params) if @user.asset @asset = @user.asset.update_attributes(:asset_one_type => asset::image) else @asset = @user.create_asset(:asset_one_type => asset::image) end @incompletearray = formfinished @user if @incompletearray.length > 0 @incomplete = true else @incomplete = false end if @incompletearray.length == 0 p 'its doing this' redirect_to action: 'show' else p 'its doing else' flash.now[:notice] = 'please fill out required fields.' redirect_to action: 'edit' , incomplete: @incomplete end end
what this, might need check if update succeded before doing else. , if works still have 2 options. incompletearray empty or not.
def update if @user.update(user_params) if @user.asset @asset = @user.asset.update_attributes(:asset_one_type => asset::image) else @asset = @user.create_asset(:asset_one_type => asset::image) end incompletearray = formfinished @user # do? incomplete = incompletearray.length > 0 if @incompletearray.length == 0 p 'its doing this' redirect_to action: 'show' else p 'its doing else' flash.now[:notice] = 'please fill out required fields.' redirect_to action: 'edit' , incomplete: incomplete end else #whatever happens when @user.update fails end end
also don't utilize many @variables if not using them in view.
ruby-on-rails
Comments
Post a Comment