# How do I determine the "source" action for conditionally rendering error actions and validation mes

**URL:** <https://www.ruby-forum.com/t/how-do-i-determine-the-source-action-for-conditionally-rendering-error-actions-and-validation-mes/185295>\
**Category:** Rails\
**Created:** [March 17, 2010, 4:43pm UTC](https://www.ruby-forum.com/t/how-do-i-determine-the-source-action-for-conditionally-rendering-error-actions-and-validation-mes/185295 "2010-03-17T16:43:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clay\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Clay\_H](https://www.ruby-forum.com/u/Clay_H)\
**Post date:** [March 17, 2010, 4:43pm UTC](https://www.ruby-forum.com/t/how-do-i-determine-the-source-action-for-conditionally-rendering-error-actions-and-validation-mes/185295/1 "2010-03-17T16:43:47Z")

</div>

I have a model called Stock.rb. I created an additional action, called  
dispense, in the controller so that I could render two different views  
based on the type of data entry being done. The “new” action is used  
for “adding stock” and the “dispense” action is used for “dispensing  
stock.”

They are very similar, but use different form partials to remove  
fields that could potentially cause confusion. Both actions use the  
“create” action when submitted.

Stock.rb contains  
validates\_presence\_of :medicine\_id, :route\_id

stocks\_controller.rb contains

def create  
@stock = Stock.new(params[:stock])  
@routes = Route.find(:all, :capitalize, :order =\> :name)  
@medicines = Medicine.find(:all, :capitalize, :order =\> :name)  
if @stock.save  
flash[:notice] = “Success | Sucesso | Éxito | Réussi”  
redirect\_to(:back)  
else  
flash[:error] = “You must select a medicine and a route.”  
redirect\_to(:back)  
#render :action =\> ‘new’  
end  
end

Each of the form partials contains  
\<%= f.error\_messages %\>  
so that validation errors are shown at the top of the form if the user  
fails to select a medicine or a route.

The problem I’m having is that the validation error messages are only  
shown when using render.  
render :action =\> ‘new’ shows the error messages, but if the user  
comes to this from the “dispense” action, they are returned to the  
form for the “new” action, which is used for a different purpose.

I changed the code to redirect\_to(:back), which returns the user to  
the proper form, but doesn’t show the validation error message – only  
the flash[:error] message that I inserted.

How do I either:

1. Detect which action/view/template sent the user to the create  
action, so that I can conditionally return them to the proper action  
using render?

or

1. Use redirect\_to(:back) and still show the validation errors?

---

<div class="post-metadata">

**Author:** ![Clay\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Clay\_H](https://www.ruby-forum.com/u/Clay_H)\
**Post date:** [March 17, 2010, 4:56pm UTC](https://www.ruby-forum.com/t/how-do-i-determine-the-source-action-for-conditionally-rendering-error-actions-and-validation-mes/185295/2 "2010-03-17T16:56:05Z")

</div>

On 17 March 2010 15:41, Clay H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a model called Stock.rb. I created an additional action, called  
> dispense, in the controller so that I could render two different views  
> based on the type of data entry being done. The “new” action is used  
> for “adding stock” and the “dispense” action is used for “dispensing  
> stock.”
> 
> They are very similar, but use different form partials to remove  
> fields that could potentially cause confusion. Both actions use the  
> “create” action when submitted.

Not answering your question directly, but you might considered an  
alternative approach, which is to use the ‘new’ action for both adding  
and dispensing, with a parameter in the url to indicate which is  
required (or possibly you can already tell which action is required by  
examining the existing parameters). You say the two actions are very  
similar so this might make for less code and would solve the problem  
you are having with the render I think.

Colin

---

<div class="post-metadata">

**Author:** ![Clay\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Clay\_H](https://www.ruby-forum.com/u/Clay_H)\
**Post date:** [March 17, 2010, 4:57pm UTC](https://www.ruby-forum.com/t/how-do-i-determine-the-source-action-for-conditionally-rendering-error-actions-and-validation-mes/185295/3 "2010-03-17T16:57:15Z")

</div>

On 17 March 2010 15:41, Clay H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How do I either:
> 
> 1. Detect which action/view/template sent the user to the create  
> action, so that I can conditionally return them to the proper action  
> using render?
> 
> or
> 
> 1. Use redirect\_to(:back) and still show the validation errors?

The RESTful way is to have a “dispense\_stock” controller - which has  
its own methods.

The cheaty (smelly) way is to put a hidden field in the form or an  
extra querystring parameter with with the value “new” or “dispense”,  
and check the params hash for the value.

---

<div class="post-metadata">

**Author:** ![Clay\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Clay\_H](https://www.ruby-forum.com/u/Clay_H)\
**Post date:** [March 17, 2010, 5:08pm UTC](https://www.ruby-forum.com/t/how-do-i-determine-the-source-action-for-conditionally-rendering-error-actions-and-validation-mes/185295/4 "2010-03-17T17:08:57Z")

</div>

On 17 March 2010 15:55, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The RESTful way is to have a “dispense\_stock” controller - which has  
> its own methods.
> 
> The cheaty (smelly) way is to put a hidden field in the form or an  
> extra querystring parameter with with the value “new” or “dispense”,  
> and check the params hash for the value.

Whether this is smelly or not really depends on how similar the two  
actions are. As I understand it they both create a new record in the  
same table, so it _could_ be virtually (or even completely) odour free  
to use the same action and determine the details of what to do based  
on parameters (it may not even be necessary to add a new parameter, it  
may be implicitly clear from existing parameters). On the other hand  
I agree that route could cause an offensive effluvia to exuded. It  
all depends on the detail.

Colin

---

<div class="post-metadata">

**Author:** ![Clay\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Clay\_H](https://www.ruby-forum.com/u/Clay_H)\
**Post date:** [March 17, 2010, 5:27pm UTC](https://www.ruby-forum.com/t/how-do-i-determine-the-source-action-for-conditionally-rendering-error-actions-and-validation-mes/185295/5 "2010-03-17T17:27:41Z")

</div>

On 17 March 2010 16:06, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > The RESTful way is to have a “dispense\_stock” controller - which has  
> > its own methods.
> > 
> > The cheaty (smelly) way is to put a hidden field in the form or an  
> > extra querystring parameter with with the value “new” or “dispense”,  
> > and check the params hash for the value.
> 
> Whether this is smelly or not really depends on how similar the two  
> actions are.

Don’t get me wrong - I wasn’t saying _don’t_ use a hidden param - I  
was suggesting it as a perfectly acceptable option… it’s just that I  
know the REST police will jump on you if you’re not careful.  
To be honest, in lots of situations, the quick, easy (dirty?) way is  
best _because_ it’s quick and easy.

And you’re right; it may be perfectly possible to design the form to  
use the same, restful action, and please everybody 🙂

---

<div class="post-metadata">

**Author:** ![Clay\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Clay\_H](https://www.ruby-forum.com/u/Clay_H)\
**Post date:** [March 17, 2010, 6:29pm UTC](https://www.ruby-forum.com/t/how-do-i-determine-the-source-action-for-conditionally-rendering-error-actions-and-validation-mes/185295/6 "2010-03-17T18:29:44Z")

</div>

On Mar 17, 12:06 pm, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > using render?  
> > > and check the params hash for the value.  
> > > Colin  
> > > Both of your comments were helpful and led to me to consider how I  
> > > could check the form elements to determine where to send them upon  
> > > validation failure. It ain’t restful and probably smells quite  
> > > unpleasant, but it seems to work. I’ll keep an eye on it and improve  
> > > the structure once I’ve straightened up some other issues. Thanks!
