0

I have a controller that I want to refactor. This controller basically renders a formatted XML document.

The controller does 4 main things.

  1. It get records from a DB
  2. It reads a static xml file from rails_root/config
  3. It iterates through DB records and gsubs the XML doc using the DB data
  4. It renders the XML document as :text

My question is:

Where is the correct place for 2/3 to happen within in the Rails framework? It seems to me that they shouldn't be in the controller.

I'm on Rails 2.3

Cheers

1 Answer 1

1

I don't know what best practice is, but in my case I'd do 1, 2, and 3 in one class method in the model. I'd create a model method that takes in the name of the base XML file.

So I'd do something like this... (CAVEAT: I'm not 100% sure about this being syntactically correct, I'm still kind of new to Rails)

# app/models/mymodel.rb
class MyModel < ActiveRecord::Base

  # ...

  def self.formatXml(xml_base)
    dataRecords = MyModel.find(:whatever) # step 1

    # open the XML and read it into a string, this is step 2
    # take the XML string, do the appropriate gsubs

    return my_xml_string;
  end
end

# app/controllers/mycontroller.rb
class MyController < ActionController::Base

  # ...

  def myAction
    formattedXml = MyModel.formatXml(whateverXmlFileName)

    render :text => formattedXml
  end
end
Sign up to request clarification or add additional context in comments.

Comments

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.