ruby on rails problem

Discussion in 'Ruby' started by Ken Fettig, Jun 18, 2005.

  1. Ken Fettig

    Ken Fettig Guest

    Hello, I have a Ruby on Rails question. I have used a layout in a
    controller, and I have image files referenced in the layout. I have the
    image files in the public/images dir. When I reference the controller in a
    URL directly ( http://127.0.0.1:3000/students ), Rails finds the image
    files. When I reference it indirectly like the following (
    http://127.0.0.1:3000/students ), Rails does not find the image files. I am
    using the Webrick server. Does anyone know what I am doing wrong, or if
    there is a bug that I do not know of? How do I work around this?

    Thanks Much
    Ken
    Ken Fettig, Jun 18, 2005
    #1
    1. Advertising

  2. The URLs you pasted are identical so I'm not sure how relevant this
    answer is, but here goes:

    Whenever I reference images, I do so like so:

    /images/image.gif (the first / being very important - it indicates
    "starting at the base URL" or "from the root").

    That way, regardless of what context the layout is executing in, the
    browser will always look for the image off the root URL (like
    http://127.0.0.1:3000/images/image.gif).

    On 6/17/05, Ken Fettig <> wrote:
    > Hello, I have a Ruby on Rails question. I have used a layout in a
    > controller, and I have image files referenced in the layout. I have the
    > image files in the public/images dir. When I reference the controller in =

    a
    > URL directly ( http://127.0.0.1:3000/students ), Rails finds the image
    > files. When I reference it indirectly like the following (
    > http://127.0.0.1:3000/students ), Rails does not find the image files. I=

    am
    > using the Webrick server. Does anyone know what I am doing wrong, or if
    > there is a bug that I do not know of? How do I work around this?
    >=20
    > Thanks Much
    > Ken
    >=20
    >=20
    >=20
    >
    Michael Buffington, Jun 18, 2005
    #2
    1. Advertising

  3. Ken Fettig

    Ken Fettig Guest

    Correction: I reference it indirectly like
    http://127.0.0.1:3000/students/list .
    "Ken Fettig" <> wrote in message
    news:...
    > Hello, I have a Ruby on Rails question. I have used a layout in a
    > controller, and I have image files referenced in the layout. I have the
    > image files in the public/images dir. When I reference the controller in a
    > URL directly ( http://127.0.0.1:3000/students ), Rails finds the image
    > files. When I reference it indirectly like the following (
    > http://127.0.0.1:3000/students ), Rails does not find the image files. I
    > am using the Webrick server. Does anyone know what I am doing wrong, or
    > if there is a bug that I do not know of? How do I work around this?
    >
    > Thanks Much
    > Ken
    >
    Ken Fettig, Jun 19, 2005
    #3
  4. Ken Fettig

    Ken Fettig Guest

    Thank you so much!!!! That did the trick!!!!! Much appreciated!


    "Michael Buffington" <> wrote in message
    news:...
    The URLs you pasted are identical so I'm not sure how relevant this
    answer is, but here goes:

    Whenever I reference images, I do so like so:

    /images/image.gif (the first / being very important - it indicates
    "starting at the base URL" or "from the root").

    That way, regardless of what context the layout is executing in, the
    browser will always look for the image off the root URL (like
    http://127.0.0.1:3000/images/image.gif).

    On 6/17/05, Ken Fettig <> wrote:
    > Hello, I have a Ruby on Rails question. I have used a layout in a
    > controller, and I have image files referenced in the layout. I have the
    > image files in the public/images dir. When I reference the controller in a
    > URL directly ( http://127.0.0.1:3000/students ), Rails finds the image
    > files. When I reference it indirectly like the following (
    > http://127.0.0.1:3000/students ), Rails does not find the image files. I
    > am
    > using the Webrick server. Does anyone know what I am doing wrong, or if
    > there is a bug that I do not know of? How do I work around this?
    >
    > Thanks Much
    > Ken
    >
    >
    >
    >
    Ken Fettig, Jun 19, 2005
    #4
  5. Michael Buffington wrote:
    > The URLs you pasted are identical so I'm not sure how relevant this
    > answer is, but here goes:
    >
    > Whenever I reference images, I do so like so:
    >
    > /images/image.gif (the first / being very important - it indicates
    > "starting at the base URL" or "from the root").
    >
    > That way, regardless of what context the layout is executing in, the
    > browser will always look for the image off the root URL (like
    > http://127.0.0.1:3000/images/image.gif).


    The problem with this solution is it only works if your app is running
    as document root, which will be the case if you're running under
    Webrick, but not necessarily otherwise. If you want your app to be able
    to work correctly even when not installed under document root, use the
    image_tag helper function instead of absolute URLs. That is, instead of
    writing

    <img src="/images/image.gif"/>

    Write:

    <%= image_tag "image.gif" %>

    If your app is installed under somewhere other than document root, this
    will still expand to a valid url. See the documentation for the
    ActionView::Helpers::AssetTagHelper module for other similar helper
    functions for javascript files and stylesheets.

    Here's a more general helper function I use to generate links to any
    static file. Add this function to the ApplicationHelper module in
    app/helpers/application_helper.rb:

    def link_to_file(name, file, *args)
    if file[0] != ?/
    file = "#{@request.relative_url_root}/#{file}"
    end
    link_to name, file, *args
    end

    Which you'd use like:

    <%= link_to_file "Installer Program", "installer.exe" %>

    Which would generate

    <a href="/installer.exe">Installer Program</a>

    under Webrick, but if your app is installed under Apache using FCGI, at
    say http://your.host.com/yourapp/, then the generated link would be

    <a href="/yourapp/public/installer.exe">Installer Program</a>

    without any changes to your code.
    Adam P. Jenkins, Jun 20, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    176
    Michael Campbell
    Mar 17, 2005
  2. Michael Schuerig
    Replies:
    3
    Views:
    214
    Carl Youngblood
    Jun 17, 2005
  3. Josef 'Jupp' SCHUGT
    Replies:
    5
    Views:
    161
    Stephan Kämper
    Jan 24, 2006
  4. Nathaniel Brown
    Replies:
    2
    Views:
    126
    gabriele renzi
    Mar 26, 2006
  5. Jason Wong
    Replies:
    0
    Views:
    160
    Jason Wong
    Nov 20, 2007
Loading...

Share This Page