Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Problems mergin two actions with different type of reuqests
4 Posts
Problems mergin two actions with different type of reuqests

Well, you can't have just multiple values in the ternary operator, so you'll need to have arrays in there and flatten them into args with the * operator:

form_tag( *( params[:id].blank? ? { :action => 'create' } : [ { :action => 'update', :id => @category }, { :method => :put } ] ))

But again, not the recommended way to go.

Also, unless you're in an ancient version of Rails then you should also try using form_for, which requires no such logic:


And it will do the right thing depending on whether @@category@ has a defined @.id@ or not.

thank you very much Jason I guess my question is really... how to pass create using post method and update using put method, all inside of the ternary operator?

The ternary operators @?:@ bind tighter than the comma operator meaning that your @form_tag@ line with parens showing binding:

 'create' } : { :action => 'update', :id => @category } ), :method => :put ) do %>

So, you can see that the @:method@ is being set to @:put@ whether @params[:id]@ is blank or not.

FWIW, it's rare to go to this much trouble to DRY up your view code. It's quite common to have the form fields in a partial which you pull in both views, but you're not usually gaining much by putting any more than that.

Hi, I trying to merge create and edit actions in one view. The problem is only one function will work at a time. If I removed the ":method => :put" action create will function and when i incorporated it action update will work but not create? Your help is much appreciated!

'create'} :

{:action => 'update', :id => @category}, :method => :put) do %>


4 Posts
Login to add your message