Rails best practice on conditional parameters in a controller action

Posted by randombits on Stack Overflow See other posts from Stack Overflow or by randombits
Published on 2010-04-18T03:08:27Z Indexed on 2010/04/18 3:13 UTC
Read the original article Hit count: 261

Filed under:

I have a controller create action looks for one or more parameters in the following ruleset. Let's say we have two parameters, foo and bar. The rules are the following:

1) if foo doesn't exist in the parameter list, bar must.
2) if bar doesn't exist in the parameter list, foo must.
3) they can both co-exist. they can't both be omitted (that's redundant with my rules above :) )

Can anyone show an example in Rails on how this is handled in the controller? Should I use a before_filter? Would appreciate some guidance as this isn't something that ActiveRecord validates.. so I'd need to build an error message to the user directly from controller logic, not model logic.

For bonus points, I output the error in XML, so if you can show how that's done, that'd be great. Hypothetically let's call the resource "Lorem", so it is created via http://foo/lorem.xml and we have lorem_controller.rb.

© Stack Overflow or respective owner

Related posts about ruby-on-rails