title: Rails best practice on conditional parameters in a controller action
body: <p>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:</p>
  
  <p>1) if foo doesn't exist in the parameter list, bar must.<br>
  2) if bar doesn't exist in the parameter list, foo must.<br>
  3) they can both co-exist. they can't both be omitted (that's redundant with my rules above :) )</p>
  
  <p>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. </p>
  
  <p>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 <a href="http://foo/lorem.xml" rel="nofollow noreferrer">http://foo/lorem.xml</a> and we have lorem_controller.rb.</p>
  
  <p>I'm trying something like this, hoping there's a cleaner way:</p>
  
  <pre><code>  def create
      logger.info("request bootstrap: #{params}")
      if params[:foo].nil? &amp;&amp; params[:bar].nil?
        @errors = [] ||= &lt;&lt; "Must include foo or bar."
        respond_to do |format|
          format.xml { render :layout =&gt; false,
                        :xml =&gt; @errors.to_xml }
        end
      end
    end
  </code></pre>