# Conditions in form\_for

**URL:** <https://www.ruby-forum.com/t/conditions-in-form-for/183120>\
**Category:** Rails\
**Created:** [February 8, 2010, 3:47pm UTC](https://www.ruby-forum.com/t/conditions-in-form-for/183120 "2010-02-08T15:47:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 8, 2010, 3:47pm UTC](https://www.ruby-forum.com/t/conditions-in-form-for/183120/1 "2010-02-08T15:47:42Z")

</div>

Hey guys,

How can we give two form\_for based on conditions? I mean like this:-

unless condition1.nil?  
form\_for :abc, :url =\> { :action =\> “123” } do |f|  
else  
form\_for :abc, :url =\> { :action =\> “234” } do |f|  
end

This is not working. Can you guyz tell me where is the problem ? How can  
i write this scenario.

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 8, 2010, 3:52pm UTC](https://www.ruby-forum.com/t/conditions-in-form-for/183120/2 "2010-02-08T15:52:37Z")

</div>

Hemant B. wrote:

> Hey guys,
> 
> How can we give two form\_for based on conditions? I mean like this:-
> 
> unless condition1.nil?  
> form\_for :abc, :url =\> { :action =\> “123” } do |f|  
> else  
> form\_for :abc, :url =\> { :action =\> “234” } do |f|  
> end
> 
> This is not working. Can you guyz tell me where is the problem ?

The problem is that form\_for takes a block. You can’t write only half a  
block before else.

Also, unless…else doesn’t make sense. Use if…else.

> How can  
> i write this scenario.

In the controller:

@action = condition1.nil? ? ‘234’ : ‘123’

And in the view:

form\_for :abc, :url =\> {:action =\> @action} do |f|

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 8, 2010, 4:37pm UTC](https://www.ruby-forum.com/t/conditions-in-form-for/183120/3 "2010-02-08T16:37:49Z")

</div>

> @action = condition1.nil? ? ‘234’ : ‘123’

Thanks for a quickie dude… Resolved … 🙂

> And in the view:
> 
> form\_for :abc, :url =\> {:action =\> @action} do |f|
> 
> Best,  
> –Â  
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid
