0

i use acts-as-taggable-on for tagging.

apartments_controller

def index
  if params[:tag]
    @apartments = Apartment.tagged_with(params[:tag])
  else
    @apartments = Apartment.all
  end    
end

routes

resources :apartments do
  #...
  collection do
    get ':tag', to: 'apartments#index', as: :tag
  end
  #...

I get nice urls by example /apartments/tag1 etc.

I want to show custom content based on the tag name in the apartments index template.

Apartment's index view:

- @appartments.each do |tags|
   - case tags.tag_list
   - when "tag1"
      %p tag1 content
   - when "tag2"
      %p tag2 content
   - else
      %p default content

When i go to url apartments/tag1 the text "default content" is show and not "tag1 content".

What am I doing wrong?

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
Remco
  • 681
  • 1
  • 6
  • 20

1 Answers1

1

There several notes about your code:

  1. Keep your logic away from views. I.e. extract code into helper methods.
  2. Do not use case on Enumerable, in your case it seems like an array. Use include? to check whether element is present inside an array:

1.9.3p194 :001 > a= [:a, :b, :c]
 => [:a, :b, :c] 
1.9.3p194 :002 > case a
1.9.3p194 :003?>   when :a
1.9.3p194 :004?>   p '1'
1.9.3p194 :005?>   when :b
1.9.3p194 :006?>   p '2'
1.9.3p194 :007?>   when :c
1.9.3p194 :008?>   p '3'
1.9.3p194 :009?>   when :d
1.9.3p194 :010?>   p 'Never'
1.9.3p194 :011?>   end
 => nil 
1.9.3p194 :012 > a.include?(:c)
 => true 
Mark Huk
  • 2,379
  • 21
  • 28
  • i tried your suggestion, like this by example...rails c > a = Apartment.find(26) > a.tag_list > output => ["lux", "familie", "groups"] and then a.include?("lux") i get this error NoMethodError: undefined method `include?' (ruby 1.9.2p290 / activemodel-3.2.8) – Remco Nov 16 '12 at 19:07
  • `a.tag_list.include?('lux')` Seems like there typo? – Mark Huk Nov 19 '12 at 09:42