# If statement based on a action

**URL:** <https://www.ruby-forum.com/t/if-statement-based-on-a-action/52244>\
**Category:** Rails\
**Created:** [January 12, 2006, 5:59am UTC](https://www.ruby-forum.com/t/if-statement-based-on-a-action/52244 "2006-01-12T05:59:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [January 12, 2006, 5:59am UTC](https://www.ruby-forum.com/t/if-statement-based-on-a-action/52244/1 "2006-01-12T05:59:09Z")

</div>

Question for you all is it possible to use an action in an if  
statement? I am using the same form for edit and new and I have a  
cancel button on the page.

This cancel button is built with an if statement that looks right now  
to see if a parameter exists to define where to go back to if the  
user cancel. The problem occurs when the user submits the form and  
there are missing fields by validation. The form is redrawn but my  
parameter is now gone so it assumes that I was editing on the cancel  
as opposed to going back to my list page. When the user clicks on the  
button they get an error because there is no id for my object to show  
as it does not exist.

basically can you do something like this:

\<% if :action == ‘create’ %\>  
go back to the list  
\<% else %\>  
go to show something  
\<% end %\>

and if you can what is the correct syntax as that does not work  
(already tried it)

thanks again for all your help

Just trying to clean up my last few bugs/issues before the release of  
this app to beta at the beginning of the week.

Andrew

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [January 12, 2006, 6:40am UTC](https://www.ruby-forum.com/t/if-statement-based-on-a-action/52244/2 "2006-01-12T06:40:31Z")

</div>

Andrew F. wrote:

> Question for you all is it possible to use an action in an if  
> statement? I am using the same form for edit and new and I have a  
> cancel button on the page.

You want to make your form a partial and then in the ‘edit’ and ‘new’  
actions use views which wrap whatever is necessary around a call to  
render\_partial.

See  
[Peak Obsession](http://rails.rubyonrails.com/classes/ActionView/Partials.html)

–  
Jesse F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
University of Chicago - NSIT Web Services  
AIM: farmerje  
Jabber: removed\_email\_address@domain.invalid  
Phone: (773)363-1058

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [January 12, 2006, 7:11am UTC](https://www.ruby-forum.com/t/if-statement-based-on-a-action/52244/3 "2006-01-12T07:11:30Z")

</div>

I _think_ @controller.action\_name is what you’re looking for. Haven’t  
checked though…

Cheers!

-DF

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [January 12, 2006, 2:10pm UTC](https://www.ruby-forum.com/t/if-statement-based-on-a-action/52244/4 "2006-01-12T14:10:28Z")

</div>

Thanks all I’ll try this last one. I am using partials to build my  
form but you can come at edit from two different places and I wanted  
to bring the user back to where they came from if they clicked  
cancel. I did get this to work:

current\_page?(:controller =\> ‘name’, :action =\> ‘create’) == true

I found that the only place the link was breaking was on the create  
because i was passing my own param list =\> true from the home page in  
order to get users to come back. However this last suggestion seems  
to be a little simplier and nicer. Thank what i found and how I was  
doing it before.

Andrew

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [January 12, 2006, 7:21am UTC](https://www.ruby-forum.com/t/if-statement-based-on-a-action/52244/5 "2006-01-12T07:21:38Z")

</div>

David F. wrote:

> I _think_ @controller.action\_name is what you’re looking for. Haven’t  
> checked though…
> 
> Cheers!
> 
> -DF

Pretty sure it’s ‘controller.action\_name’. There is also a  
‘controller.controller\_name’.

\_Kevin
