Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1ruby-on-rails-3.2haml

How to get relationship data value in my HAML template


This is a part of my house model.

class House < ActiveRecord::Base

    has_many :amenity_appartment
    has_many :amenities, :through => :amenity_appartment

    has_many :category_join_table
    has_many :categories, :through => :category_join_table

    has_one :price
end

Because we get more than 100 houses in our listing i want to add filtering. After searching i find great way to do that. http://www.webegg.co.uk/jquery-multiple-filter/

i want to start with 3 filter options...price, category (country home, vila ect) and amenties (dishwasher, beertap ect)

I just need to fill my template with the data value from my house relationships.

Example:

#holderwrap
  %ul#holder.filterThis
   %li.1200.design-villa.dishwasher.barbecue.wifi

(1200 euro per week - design villa with the amenities a dishwasher, barbeque and wifi)

How can i fill in the values in the LI tag?


Solution

  • In HAML, you can pass html classes in a hash:

    %li{ :class => "1200 design-villa dishwasher barbecue wifi" }
    

    You would need get the amenity names, translate them into their respective html classes (if necessary), and join into a string. You could probably package this all into a House instance method:

    def features_to_html_class
      "#{price} #{(amenities + categories).map(&:name).join(' ')}"
    end
    

    And in your view:

    %li{ :class => house.features_to_html_class }