Tuesday, November 23, 2010

Ruby on Rails: Using Multiple Subdirectories in Views

Hello,

I was working on a rails project when I realized that I wanted to use multiple subdirectories in views for the same controller, in order to clarify inside the url where we were navigating.

For example, for a controller vehicles, I could want to make three subcategories (trucks, vans and cars), and inside each subcategory, i could add more views (cars -> latest, cars -> old, cars -> red, ...)

For that purpose I wanted to have a view directory structure like this:

trucks
  - new
  - old
  - red
vans
  - new
  - old
  - red
cars
  - new
  - old
  - red

In order to accomplish this in an easy way (not modifying the routes.rb nor anything similar), the easy way is to use the following code in your vehicles controller:

def cars
    render :action =>
"cars/index"
end

This way, you can call all the actions you want, organize them in any subdirectory structure, and keep your code clean.

Tuesday, May 4, 2010

RoR & Lightbox Issue: Close and Loading Pictures

I recently wasted over 2 hours because the lightbox script stopped working when I modified my views in Ruby on Rails.

The close and reload pictures weren't showing anymore, and this was not acceptable for publishing.

I worked over with a previous version of my code which did work, but no matter how many lines I copied from one place to another, my new code wouldn't work.

In the end, I checked the own lightbox.js content (I know little about javascript, so I didn't consider it an option until I was desperate). And the problem was there.

Lines 49 and 50 where:
     fileLoadingImage:             '../images/loading.gif',
     fileBottomNavCloseImage:
'../images/closelabel.gif',

This worked in my initial version because my view was http://localhost:3000/es/view_boat?model=First-53F5 so the ../images worked like charm.

But my new view was http://localhost:3000/es/barcos/1  So ../image failed. I first changed lines 49 and 50 of lightbox.js to ../../images/*.gif, but that was not optimal, so in the end I left them like this:
     fileLoadingImage:             '/images/loading.gif',
     fileBottomNavCloseImage:
'/images/closelabel.gif',

And that was it, here goes my first post!