# Posts/gets to other servers inside of action?

**URL:** <https://www.ruby-forum.com/t/posts-gets-to-other-servers-inside-of-action/138804>\
**Category:** Rails\
**Created:** [May 27, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/posts-gets-to-other-servers-inside-of-action/138804 "2008-05-27T22:31:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [May 27, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/posts-gets-to-other-servers-inside-of-action/138804/1 "2008-05-27T22:31:48Z")

</div>

Hi all,

We are in a ‘web-service-like’ scenario in which a controller action  
needs to save some things to the database and issue a couple of  
POSTS/GETS to other servers. These aren’t web services, per se, but  
rather perl scripts on a wiki. My solution feels clunky, and I wondered  
if there’s a cleaner way.

class AccountController  
require ‘net/http’  
require ‘uri’

def create  
# Create account in DB  
…  
account.save!

```
# Create account page on wiki
wiki_url = URI.parse('http://ourwiki/save_page')
result = Net::HTTP.post_form(wiki_url, {:page_name => account.name +

```

‘Page’})  
raise “Couldn’t create” unless response.body =~ /Welcome to  
#{account.name}/

```
redirect_to :action => 'show', :id => account.id

```

end  
end

Thanks.

Brian

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [May 28, 2008, 6:01am UTC](https://www.ruby-forum.com/t/posts-gets-to-other-servers-inside-of-action/138804/2 "2008-05-28T06:01:31Z")

</div>

Brian H. wrote:

> Hi all,
> 
> We are in a ‘web-service-like’ scenario in which a controller action  
> needs to save some things to the database and issue a couple of  
> POSTS/GETS to other servers. These aren’t web services, per se, but  
> rather perl scripts on a wiki. My solution feels clunky, and I wondered  
> if there’s a cleaner way.
> 
> class AccountController  
> require ‘net/http’  
> require ‘uri’
> 
> def create  
> # Create account in DB  
> …  
> account.save!
> 
> ```
> # Create account page on wiki
> wiki_url = URI.parse('http://ourwiki/save_page')
> result = Net::HTTP.post_form(wiki_url, {:page_name => account.name +
> 
> ```
> 
> ‘Page’})  
> raise “Couldn’t create” unless response.body =~ /Welcome to  
> #{account.name}/
> 
> ```
> redirect_to :action => 'show', :id => account.id
> 
> ```
> 
> end  
> end

First, I would move this to the model.

after\_save :create\_wiki\_page  
def create\_wiki\_page  
Net::Http.post\_form(…)  
end

And if the interface got to be more than this one point of connection, I  
would move it to an API library

module WikiApi  
def self.create\_user\_page(user)  
…  
end

```
def self.create_project_page(user, project)
  ...
end

```

end

# in the model

after\_save :create\_wiki\_page  
def create\_wiki\_page  
WikiApi.create\_user\_page(self)  
end

def create\_project(project\_attributes)  
p = user.projects.create(project\_attributes)  
WikiApi.create\_project\_page(user, p)  
end

Or something…

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [May 28, 2008, 6:17pm UTC](https://www.ruby-forum.com/t/posts-gets-to-other-servers-inside-of-action/138804/3 "2008-05-28T18:17:44Z")

</div>

Alex W. wrote:

> Brian H. wrote:
> 
> > Hi all,

[…]

> Or something…

Makes perfect sense. Thanks!
