# Absolute url to images from the model?

**URL:** <https://www.ruby-forum.com/t/absolute-url-to-images-from-the-model/183231>\
**Category:** Rails\
**Created:** [February 10, 2010, 8:46am UTC](https://www.ruby-forum.com/t/absolute-url-to-images-from-the-model/183231 "2010-02-10T08:46:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [February 10, 2010, 8:46am UTC](https://www.ruby-forum.com/t/absolute-url-to-images-from-the-model/183231/1 "2010-02-10T08:46:55Z")

</div>

I have a need to pass full, absolute urls to images for an interaction  
with Facebook. I dont think I can use the view helpers (e.g.  
image\_tag) from the model though…

Is there a way to do this from the model (or less desirable, from the  
controller)?

or if I cant use image\_tag, is there another way to build these urls  
from the model?

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [February 10, 2010, 3:26pm UTC](https://www.ruby-forum.com/t/absolute-url-to-images-from-the-model/183231/2 "2010-02-10T15:26:05Z")

</div>

Your data model layer should not be aware of application state (or even  
assume it is part of a web app). So, this should be done from the  
controller/view layer.

On Feb 10, 2010 2:45 AM, “lunaclaire” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

I have a need to pass full, absolute urls to images for an interaction  
with Facebook. I dont think I can use the view helpers (e.g.  
image\_tag) from the model though…

Is there a way to do this from the model (or less desirable, from the  
controller)?

or if I cant use image\_tag, is there another way to build these urls  
from the model?

–  
You received this message because you are subscribed to the Google  
Groups  
“Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [February 10, 2010, 5:30pm UTC](https://www.ruby-forum.com/t/absolute-url-to-images-from-the-model/183231/3 "2010-02-10T17:30:05Z")

</div>

Mat B. wrote:

> Your data model layer should not be aware of application state (or even  
> assume it is part of a web app). So, this should be done from the  
> controller/view layer.
> 
> On Feb 10, 2010 2:45 AM, “lunaclaire” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> I have a need to pass full, absolute urls to images for an interaction  
> with Facebook. I dont think I can use the view helpers (e.g.  
> image\_tag) from the model though…
> 
> Is there a way to do this from the model (or less desirable, from the  
> controller)?
> 
> or if I cant use image\_tag, is there another way to build these urls  
> from the model?
> 
> –  
> You received this message because you are subscribed to the Google  
> Groups  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

If interaction with Facebook requires an absolute path to an image, and  
this Facebook interaction is part of his app’s “domain logic,” a model  
is perfectly appropriate place for this…

To do what you’re asking, you’ll need to first set

config.action\_controller.asset\_host = “http://your\_assets.domain”

Then, to use image\_tag or image\_path, include  
ActionView::Helpers::AssetTagHelper into your model (or extend some  
object with it).

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [February 10, 2010, 5:39pm UTC](https://www.ruby-forum.com/t/absolute-url-to-images-from-the-model/183231/4 "2010-02-10T17:39:53Z")

</div>

Mat B. wrote:

> Your data model layer should not be aware of application state (or even  
> assume it is part of a web app). So, this should be done from the  
> controller/view layer.

More regards to this: I am _deathly afraid_ of communicating with web  
services during my apps’ request/response cycles; you have no idea how  
long it will take for the service to respond back to you, and when  
something with the service goes wrong (after possibly minutes of  
waiting), it tends to go wrong in completely unexpected and magical ways  
(often you’ll not even _get_ a timeout!). Doing this kind if ish in a  
background service is trivial if this logic already exists in a model;  
its impossible if the logic is in a controller.

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [February 10, 2010, 7:20pm UTC](https://www.ruby-forum.com/t/absolute-url-to-images-from-the-model/183231/5 "2010-02-10T19:20:51Z")

</div>

On Wed, Feb 10, 2010 at 11:39, Paul H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## (often you’ll not even _get_ a timeout!). Doing this kind if ish in a background service is trivial if this logic already exists in a model; its impossible if the logic is in a controller.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> –  
> You received this message because you are subscribed to the Google G. “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid.  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

When we use the word “model”, we can mean different things - a narrow  
definition would be an object representing persistent data, e.g. an  
ActiveRecord instance. We also use it more broadly to mean “a class  
encapsulating business logic”.

I’m all for skinny controllers, but I think for non-trivial apps it’s  
important to make a distinction between the data model (which should  
really just be in the business of representing data) and other things  
that encapsulate business logic - e.g., the Command pattern. That  
pattern is likely appropriate for the Facebook task here - and of  
course, if you encapsulate business tasks in a Command object, they  
can be trivially called either synchronously in the controller or  
asynchronously in a background job. And, for what it’s worth,  
background jobs are a great example of why the data model shouldn’t  
assume it’s running inside a web app. The use of the asset\_host  
configuration is a common pattern and it works, but in the end it’s  
really just a fancy way of setting a global variable.

In summary: agreed entirely that the logic should be in the model, if  
by “model” we mean “a class that isn’t the controller”. And also  
agreed entirely that a background job is the right place to do this.  
But I maintain that the _data_ model shouldn’t access, or assume the  
existence of, web application state.

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [February 10, 2010, 8:14pm UTC](https://www.ruby-forum.com/t/absolute-url-to-images-from-the-model/183231/6 "2010-02-10T20:14:42Z")

</div>

Thx for the replies, Mat and Paul.

I agree w Paul in this case. The model is responsible for the  
communication w FB as part of a cross-posting step that is part of the  
business logic. So, that’s where I’m putting this.

And it kinda works… it _does_ work as far as being able to call  
image\_path from the model, but…

Something odd is going on in that I included the line

config.action\_controller.asset\_host = “http://your\_assets.domain”

with an absolute IP addrs (domain is not established yet) in both my  
config/development.rb and config/production.rb files but when I run in  
dev mode the urls always come out like:

[http://localhost:3000//images/newbie.gif](http://localhost:3000//images/newbie.gif)

without the IP addrs at the front. I’ve rebooted rails to make sure  
this config is getting picked up.

Any ideas why I keep getting localhost?
