0

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

[EDIT: Nevermind, thought this was a batch update situation] Hmm, how would you solve it in an ideal, restful way? AJAX calls for every action (or, worse yet, force a page refresh for each action)? Imagine something like a case where you have email aliases associated with an account, and want to determine which ones receive notifications.
I'd like to present the user with more or less the same visual every time they vist the page, and I'd like to always have it at the same URL. I see your point about my current plan being unrestful, though. Can you suggest a more restful solution?
I've given up the idea of having the final URL be the same, and instead have a URL that decides whether to redirect to the new record page or the edit record page. There are now separate controller actions for create, update, and destroy. It also means I no longer use the radio buttons, but I really appreciate your input, @Thiago.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.