Forum: Ruby on Rails Conditions in form_for

Posted by Hemant Bhargava (hemant-bhargava7)
on 2010-02-08 15:47
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.
Posted by Marnen Laibow-Koser (marnen)
on 2010-02-08 15:52
Hemant Bhargava 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
marnen@marnen.org
Posted by Hemant Bhargava (hemant-bhargava7)
on 2010-02-08 16:37
> 
> @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
> marnen@marnen.org
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.