views:

72

answers:

2

I am a newbie to Amazon SQS and ruby on rails. And i am working on a project that some XML messages must be send to SQS. How do i do that?

Now i have this in the controller after the .save

 def create

    @thing = Thing.new(params[:thing])

    respond_to do |format|

      if @thing.save
        message = @thing.to_xml

and in the model

inputqueue.send_message(message) 

Is this the way i can send an XML file to SQS or??

A: 

I'm not sure I understand exactly, but let me try and get this straight. Every time you create a particular model, you'd like to send an XML message off to Amazon SQS? If that's the case, then...

keep your controller as so:

def create
  @thing = Thing.new(params[:thing])

  if @thing.save
    #render view/partial/other
  else
    #display errors to user
end

Then, you'll use an Observer to make the call to Amazon. Put your observer right inside your models directory:

/app/models/Thing.rb
/app/models/ThingObserver.rb

Your observer might look something like:

class ThingObserver < ActiveRecord::Observer
  def after_create(thing)
    AmazonSQSPlugin.send(thing.to_xml)
  end
end

The code in the observer will be run after every create. If the after_create in the observer returns false, the entire create is rolled back, as if it never happened.

You might have to edit your environment config to get the observer to fire though, depending on how your application is currently set up.

Mike Trpcic
Thanks for your help! I have the observer running. And im using the 'RightAws::SqsGen2' plugin. so i have made this def after_create(thing) RightAws::SqsGen2.send(thing.to_xml) endbut it gives an undefined method error.
bartligthart
What is the exact error?
Mike Trpcic
A: 

RightAws::SqsGen2.queue(queue_name, message) is the right way to go for sending the message.

bhups