joanwolk
joanwolk

Reputation: 1105

changing form behavior on submit based on radio button selection

I'm using Rails 3.1.0.rc5. I want to have a form with a pair of radio buttons, enable and disable, and a field to enter an integer (expire_after_days, the number of days until ticket expiration), with a hidden field for the fixed parameter subdomain_name. I'd like to be able to use the same simple form to create, edit, or delete a record, depending on which radio button is checked.

So if enable is checked, with no record found for the subdomain_name, a record would be created on form submission.

If enable is checked, and a record is found, the existing record should be updated on form submission.

And if disable is checked, the record should be deleted on form submission.

Is this a reasonable thing to do? If so, what tips do you have for how to do it?

Upvotes: 0

Views: 535

Answers (1)

Thiago Jackiw
Thiago Jackiw

Reputation: 799

It's not ideal nor restful to have all 3 actions (create, update, destroy) cramped up in just one controller method, but if you wish to continue on this dirty route here's what you could do:

def my_dirty_method
  if params[:enable].present?
    if params[:subdomain_name].present?
      # Edit subdomain
    else
      # Create subdomain
    end
  end
  if params[:disable].present?
    # Delete subdomain
  end

Upvotes: 1

Related Questions