# Url\_for\_file\_column problems

**URL:** <https://www.ruby-forum.com/t/url-for-file-column-problems/48358>\
**Category:** Rails\
**Created:** [November 19, 2005, 11:29am UTC](https://www.ruby-forum.com/t/url-for-file-column-problems/48358 "2005-11-19T11:29:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vamlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@vamlists](https://www.ruby-forum.com/u/vamlists)\
**Post date:** [November 19, 2005, 11:29am UTC](https://www.ruby-forum.com/t/url-for-file-column-problems/48358/1 "2005-11-19T11:29:15Z")

</div>

Hello,

Has anyone got file\_column working fully? it’s doing most of it’s job  
properly, except for the url\_for\_file\_column helper method. Whatever I  
do, it’s always returning a nil. I tried reading the source, but not  
entirely sure what it’s doing. Can somebody throw some light on this?

TIA,  
Vamsee.

---

<div class="post-metadata">

**Author:** ![vamlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@vamlists](https://www.ruby-forum.com/u/vamlists)\
**Post date:** [November 19, 2005, 12:17pm UTC](https://www.ruby-forum.com/t/url-for-file-column-problems/48358/2 "2005-11-19T12:17:31Z")

</div>

Sorry, don’t bother… I made a stupid mistake.

Vamsee K. wrote:

> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
If you’re not living on the edge, you’re taking up too much room - Jayne  
Howard

---

<div class="post-metadata">

**Author:** ![vamlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@vamlists](https://www.ruby-forum.com/u/vamlists)\
**Post date:** [November 19, 2005, 12:17pm UTC](https://www.ruby-forum.com/t/url-for-file-column-problems/48358/3 "2005-11-19T12:17:31Z")

</div>

On 11/19/05, Vamsee K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Has anyone got file\_column working fully? it’s doing most of it’s job  
> properly, except for the url\_for\_file\_column helper method. Whatever I  
> do, it’s always returning a nil. I tried reading the source, but not  
> entirely sure what it’s doing. Can somebody throw some light on this?

it comes with proper rdocs that should more or less explain what it is  
doing. If anybody has suggestions how to make this even more clearer  
I’m more than happy to receive them.

Regarding your question: If your model is stored in an instance (!)  
variable “@foo” and your file\_column is called “image”, then you can  
use it like this:

\<%= url\_for\_file\_column “foo”, “image” %\>

Hope this helps  
Sebastian

---

<div class="post-metadata">

**Author:** ![vamlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@vamlists](https://www.ruby-forum.com/u/vamlists)\
**Post date:** [November 19, 2005, 1:27pm UTC](https://www.ruby-forum.com/t/url-for-file-column-problems/48358/4 "2005-11-19T13:27:01Z")

</div>

Sebastian K. wrote:

> Regarding your question: If your model is stored in an instance (!)  
> variable “@foo” and your file\_column is called “image”, then you can  
> use it like this:
> 
> \<%= url\_for\_file\_column “foo”, “image” %\>

Thanks Sebastian, I solved this with some help on irc. However, maybe  
I’m missing something basic, but I didn’t understand this part: I want  
to list all the images as thumbnails in the list.rhtml.

This works:

\<% for @contact in @contacts %\>

 \<%=h @contact.name %\> \<%=h @contact.address %\> \<%= image\_tag url\_for\_file\_column "contact", "picture", "thumb" %\> \<% end %\>

But this throws a nil error. Why?

\<% for contact in @contacts %\>

 \<%=h contact.name %\> \<%=h contact.address %\> \<%= image\_tag url\_for\_file\_column "contact", "picture", "thumb" %\> \<% end %\>

TIA,  
Vamsee.

---

<div class="post-metadata">

**Author:** ![vamlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@vamlists](https://www.ruby-forum.com/u/vamlists)\
**Post date:** [November 20, 2005, 4:48pm UTC](https://www.ruby-forum.com/t/url-for-file-column-problems/48358/5 "2005-11-20T16:48:28Z")

</div>

Sebastian K. wrote:

> > 
> 
> simply followed this convention, but lots of people seem to have  
> trouble with it. Perhaps, I’ll come up with something else…

I did this in my copy of file\_column\_helper.rb:

def url\_for\_file\_column(object\_name, method, suffix=nil)  
rel\_path\_method = “#{method}\_relative\_path”  
opts\_method = “#{method}\_options”  
if object\_name.respond\_to?(rel\_path\_method) &&  
object\_name.respond\_to?(opts\_method)  
object = object\_name  
else  
object = instance\_variable\_get("@#{object\_name.to\_s}")  
end  
relative\_path = object.send(rel\_path\_method, suffix)  
return nil unless relative\_path  
url = “”  
url \<\< @request.relative\_url\_root.to\_s \<\< “/”  
url \<\< object.send(opts\_method)[:base\_url] \<\< “/”  
url \<\< relative\_path  
end

Basically it’ll accept either a named instance variable, or something  
that looks like an AR instance with a named file\_column. I think this  
is closer to the convention. It certainly makes showing thumbnail lists  
easier.

---

<div class="post-metadata">

**Author:** ![vamlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@vamlists](https://www.ruby-forum.com/u/vamlists)\
**Post date:** [November 19, 2005, 2:24pm UTC](https://www.ruby-forum.com/t/url-for-file-column-problems/48358/6 "2005-11-19T14:24:21Z")

</div>

On 11/19/05, Vamsee K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<% for @contact in @contacts %\>  
> …  
> \<%= image\_tag url\_for\_file\_column “contact”, “picture”, “thumb” %\>

> But this throws a nil error. Why?
> 
> \<% for contact in @contacts %\>  
> …  
> \<%= image\_tag url\_for\_file\_column “contact”, “picture”, “thumb” %\>

because url\_for\_file\_column expects the model in an _instance_  
variable, as stated in the docs as well. This is a rails convention  
that you’ll find in other helpers as well, for example text\_field. I  
simply followed this convention, but lots of people seem to have  
trouble with it. Perhaps, I’ll come up with something else…

Sebastian

---

<div class="post-metadata">

**Author:** ![vamlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@vamlists](https://www.ruby-forum.com/u/vamlists)\
**Post date:** [December 19, 2005, 9:16pm UTC](https://www.ruby-forum.com/t/url-for-file-column-problems/48358/7 "2005-12-19T21:16:35Z")

</div>

skanthak wrote:

> On 11/19/05, Vamsee K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > \<% for @contact in @contacts %\>  
> > …  
> > \<%= image\_tag url\_for\_file\_column “contact”, “picture”, “thumb” %\>
> 
> > But this throws a nil error. Why?
> > 
> > \<% for contact in @contacts %\>  
> > …  
> > \<%= image\_tag url\_for\_file\_column “contact”, “picture”, “thumb” %\>
> 
> because url\_for\_file\_column expects the model in an _instance_  
> variable, as stated in the docs as well. This is a rails convention  
> that you’ll find in other helpers as well, for example text\_field. I  
> simply followed this convention, but lots of people seem to have  
> trouble with it. Perhaps, I’ll come up with something else…
> 
> Sebastian

I think the problem for many people is that you’re treating  
url\_for\_file\_column like a form helper. On an intuitive level, that  
method doesn’t feel like a form helper at all (unlike  
file\_column\_field). Since it returns img tag parameters and not a form  
field at all, it would probably be less awkward and more intuitive if  
you could simply say

\<%= image\_tag url\_for\_file\_column(@contact.picture) %\>

Okay, back to trying to figure out what on earth I’m doing wrong in my  
own code.

-sk

---

<div class="post-metadata">

**Author:** ![vamlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@vamlists](https://www.ruby-forum.com/u/vamlists)\
**Post date:** [December 19, 2005, 10:37pm UTC](https://www.ruby-forum.com/t/url-for-file-column-problems/48358/8 "2005-12-19T22:37:16Z")

</div>

could it also be that one of your contacts does not have an image to  
work with? I ran into that in one of my apps and i had it display some  
text if the image didn’t exist for a record…I would post the code but  
i’m not at the pc my app was on…i believe it was quivanlent to

rescue ‘no image yet’ (which might have broken the iteration which then  
led me to try…)

or

\<% if @contact.picture == nil %\> (maybe unless @contact.picture would  
work as well?)

you don't have an image

\<% end %\>

or something similar…

this was from RoR noob memory so I wouldn’t trust it too much but that’s  
what i remember from my implementation.

curtis  
[http://noobonrails.blogspot.com/](http://noobonrails.blogspot.com/)

Steve K. wrote:

> skanthak wrote:
> 
> > On 11/19/05, Vamsee K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > \<% for @contact in @contacts %\>  
> > > …  
> > > \<%= image\_tag url\_for\_file\_column “contact”, “picture”, “thumb” %\>
> > 
> > > But this throws a nil error. Why?
> > > 
> > > \<% for contact in @contacts %\>  
> > > …  
> > > \<%= image\_tag url\_for\_file\_column “contact”, “picture”, “thumb” %\>
> > 
> > because url\_for\_file\_column expects the model in an _instance_  
> > variable, as stated in the docs as well. This is a rails convention  
> > that you’ll find in other helpers as well, for example text\_field. I  
> > simply followed this convention, but lots of people seem to have  
> > trouble with it. Perhaps, I’ll come up with something else…
> > 
> > Sebastian
> 
> I think the problem for many people is that you’re treating  
> url\_for\_file\_column like a form helper. On an intuitive level, that  
> method doesn’t feel like a form helper at all (unlike  
> file\_column\_field). Since it returns img tag parameters and not a form  
> field at all, it would probably be less awkward and more intuitive if  
> you could simply say
> 
> \<%= image\_tag url\_for\_file\_column(@contact.picture) %\>
> 
> Okay, back to trying to figure out what on earth I’m doing wrong in my  
> own code.
> 
> -sk
