# Checking something in update action before saving

**URL:** <https://www.ruby-forum.com/t/checking-something-in-update-action-before-saving/174700>\
**Category:** Rails\
**Created:** [August 31, 2009, 5:29pm UTC](https://www.ruby-forum.com/t/checking-something-in-update-action-before-saving/174700 "2009-08-31T17:29:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 31, 2009, 5:29pm UTC](https://www.ruby-forum.com/t/checking-something-in-update-action-before-saving/174700/1 "2009-08-31T17:29:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 31, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/checking-something-in-update-action-before-saving/174700/2 "2009-08-31T17:43:00Z")

</div>

2009/8/31 bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â Â 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
