Checking something in update action before saving

This errors as I can’t jump out of the action with render multiple
times.

“Render and/or redirect were called multiple times in this action.
Please note that you may only call render OR redirect, and at most once
per action. Also note that neither redirect nor render terminate
execution of the action, so if you want to exit an action after
redirecting, you need to do something like “redirect_to(…) and
return”.”

Fair enuf’ - but how do I do this?

def update
params[:advert][:resort_ids] ||= [] # Fix from one of Ryan’s
railscasts for checkbox updates

if current_user.admin
  @advert.cleared_for_release =

params[:advert][:cleared_for_release] # protected with
attr_protected in model
@advert.front_page = params[:advert][:front_page]

protected with attr_protected in model

  @advert.save
end

puts "**** SUBMITTED resort count :" +

params[:advert][:resort_ids].length.to_s
puts “**** ALLOWED resort count :” +
current_user.max_allowed_resorts_per_advert.to_s

allowed_resorts = current_user.max_allowed_resorts_per_advert
requested_resorts = params[:advert][:resort_ids].length

if requested_resorts > allowed_resorts
  flash[:notice] = "Sorry you're only allowed to list against

#{allowed_resorts} resorts."
render :action => ‘edit’ # Update probably failed
validation, re edit
end

@advert = Advert.find(params[:id])
if @advert.update_attributes(params[:advert])
  flash[:notice] = "Updated. Done."
  redirect_to @advert
else
  render :action => 'edit'              # Update probably failed

validation, re edit
end
end

2009/8/31 bingo bob [email protected]:

  if current_user.admin
  puts “**** ALLOWED resort count :” +
current_user.max_allowed_resorts_per_advert.to_s

  allowed_resorts = current_user.max_allowed_resorts_per_advert
  requested_resorts = params[:advert][:resort_ids].length

  if requested_resorts > allowed_resorts
   flash[:notice] = “Sorry you’re only allowed to list against
#{allowed_resorts} resorts.”
   render :action => ‘edit’        # Update probably failed
validation, re edit

Do what the message suggests and put a return here so that it does not
go on and do the next bit as well. Or possibly better use an else here
and drop to the bottom. Or even better reorganise the logic so that
there is only one call to render :action => ‘edit’.

Colin