Multiple controller with associated view calling Application Helper to get image path... not working

19 views
Skip to first unread message

Elizabeth McGurty

unread,
May 2, 2015, 1:23:43 PM5/2/15
to rubyonra...@googlegroups.com
SORRY about the previous.... How does one edit here after POST?


Working with Rails 3.0

From numerous controllers with associated views, I need to display images.

In the views to retrieve an images I have:

 <img src="<%= get_image_path(@object.image_file_name) %> />

I have also used the Rails <img src counterpart...

get_image_path is defined in the Application helper:

module ApplicationHelper
  def get_image_path(sought_image_file_name)

    return_string = ''
    unless sought_image_file_name.blank?    ## although I do testing for blank?  image_file_name in the erb view
      return_string = "#{Rails.application.root}/public/images/item_images/" + sought_image_file_name
      ### Have tried return_string "../public and "./public and these have not worked
    end
    return return_string
  end
...

But I keep getting the error:

Started GET "/home/echomarket/echomarket.org/public/images/item_images/zv37bNYgL25LisYvnGfGkKMndfm7ZD_Chrysanthemum.jpg" for ...

ActionController::RoutingError (No route matches "/home/echomarket/echomarket.org/public/images/item_images/zv37bNYgL25LisYvnGfGkKMndfm7ZD_Chrysanthemum.jpg"):

The file definitely exists in that path, per FIleZilla and Putty

I do not understand the tie between routes.rb and multiple controllers....

Thanks,

Liz

Walter Lee Davis

unread,
May 2, 2015, 2:11:21 PM5/2/15
to rubyonra...@googlegroups.com

On May 2, 2015, at 1:23 PM, Elizabeth McGurty <emcg...@gmail.com> wrote:

> SORRY about the previous.... How does one edit here after POST?

This is a mailing list (even if you are looking at it through one of the many Web interfaces), and you can't edit mail after you send it.

>
>
> Working with Rails 3.0
>
> From numerous controllers with associated views, I need to display images.
>
> In the views to retrieve an images I have:
>
> <img src="<%= get_image_path(@object.image_file_name) %> />
>
> I have also used the Rails <img src counterpart...
>
> get_image_path is defined in the Application helper:
>
> module ApplicationHelper
> def get_image_path(sought_image_file_name)
>
> return_string = ''
> unless sought_image_file_name.blank? ## although I do testing for blank? image_file_name in the erb view
> return_string = "#{Rails.application.root}/public/images/item_images/" + sought_image_file_name
> ### Have tried return_string "../public and "./public and these have not worked
> end
> return return_string
> end
> ...
>
> But I keep getting the error:
>
> Started GET "/home/echomarket/echomarket.org/public/images/item_images/zv37bNYgL25LisYvnGfGkKMndfm7ZD_Chrysanthemum.jpg" for ...
>
> ActionController::RoutingError (No route matches "/home/echomarket/echomarket.org/public/images/item_images/zv37bNYgL25LisYvnGfGkKMndfm7ZD_Chrysanthemum.jpg"):
>
> The file definitely exists in that path, per FIleZilla and Putty

When you are trying to access a file through a Web browser, you need a **web root**-relative path, not a **filesystem root**-relative path. The web root path to a static file begins at public, so any file that exists in the your-project-folder/public folder would be at /.

This file:

/home/echomarket/echomarket.org/public/images/item_images/zv37bNYgL25LisYvnGfGkKMndfm7ZD_Chrysanthemum.jpg

Would have this URL (assuming you were accessing it from http://echomarket.org):

http://echomarket.org/images/zv37bNYgL25LisYvnGfGkKMndfm7ZD_Chrysanthemum.jpg

The Rails.root starting point is only needed if you are trying to access the file directly from your server on its filesystem using File.open or similar.

Walter

>
> I do not understand the tie between routes.rb and multiple controllers....
>
> Thanks,
>
> Liz
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8e2303ba-a76b-4605-be64-d20d5ed075d2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Elizabeth McGurty

unread,
May 2, 2015, 2:56:32 PM5/2/15
to rubyonra...@googlegroups.com
Walter,

Thank you very much....

It was not apparent to me this this group was an email group... Now I understand.  When I first discovered this site I understood it as a forum...

Anyway.... You have totally helped me.  My Application Helper now reads:


  def get_image_path(sought_image_file_name)

    return_string = ''
    unless sought_image_file_name.blank?
      return_string = "/images/item_images/" + sought_image_file_name
    end
    return_string
  end

I tested it several times and it is now working.... 

I thank you very much....

Liz


Reply all
Reply to author
Forward
0 new messages