Thanks for pointing out the error of my ways Fred. Just as a follow-up
to anyone down the line who has this problem, I’ll explain how I fixed
it for my app. First I spent some time building a new constructor for my
model that accepted a flat array as an initializer. Then I said, no,
this really belongs in the controller. But after I thought about making
a new model object in the controller and jamming things into it, I said,
that’s kind of ugly too. Really, what if I want to pass the controller
two model objects at the same time down the line? The way the Rails team
intended it is right, the submitter just needs to indicate what model
object it is sending parameters for. So I extended the Hash class like
so:
class Hash
def to_controller_post(controller)
str = “”
self.each { |key,value| str << “#{controller}[#{key}]=#{value}&” }
str.chop #Remove trailing ‘&’
end
end
Now my submit code looks like this:
def getAuditData
{
“name” => ENV[‘computername’],
“username” => ENV[‘username’],
“serial” => getSerialNumberBIOS,
“processor” => getProcessorName,
“memory” => getSystemMemory,
“image” => getImageVersion
}
end
Net::HTTP.start(‘localhost’, 3000) do |query|
query.post(“/labels”, audit_data.to_controller_post(‘label’)).body
end
It produces URLs formed like this:
http://localhost:3000/labels?label[name]=labelsystem1&label[username]=bob.smith&label[serial]=12345&label[processor]=Intel&label[memory]=2048&label[image]=30
This works fine. If you are implementing this in your own code, do
remember that you will need to turn authentication token off for the
create action by putting
protect_from_forgery :except => [:create]
in your controller definition.
As a final note, I would be using the debugger, but I can’t get the gem
installed under win32 because it requires Visual c++ 6.0 to compile,
which is not a free download. A little while back I wrestled with trying
to get it to compile under Visual C++ 9.0 Express, which is free, but
never got it working. Anyone happen to have a plug-and-play debugger
solution for win32?
Thanks again all.