1

(Preface: I am 10 months into learning Rails so this could be a really rookie mistake.)

My app has Venues which have VenueCategories associated with them. In my Venue view, I want to populate some hash tags into the share content when a user shares the Venue to Twitter. For this, I have created a method (albeit perhaps not an ideal implementation, will look to refactor later) hash_tags to be called on the VenueCategory. First I put this in VenueCategoriesHelper.rb and got NoMethodError when calling it in the console to test. I moved the method to both the Model and the Controller just to test it out but yielded the same results.

I have tried defining the method with and without passing the venue_category as a parameter.

What am I doing wrong? Relevant code below.

VenueCategoriesHelper.rb

def hash_tags(venue_category)
  hash_tags = []
  hash_tag_string = ""
  if venue_category.name == "Restaurant"
    hash_tags << "#restaurants"
  elsif venue_category.name == "Salons & Spas"
    hash_tags << ["#salons", "#spas"]
  elsif venue_category.name == "Hotels & Resorts"
    hash_tags << ["#hotels", "#resorts"]
  elsif venue_category.name == "Bars & Nightlife"
    hash_tags << ["#bars", "#nightlife"]
  end
  hash_tags.each do |h|
    hash_tag_string << "#{h} "
  end
end

show.html.slim (random snippet added to view to test

...
p = resource.venue_category.hash_tags(resource.venue_category)

#header-module
  #overall-score-container
    #overall-score-header
...

Error from Console and when running in browser

NoMethodError: undefined method `hash_tags' for #<VenueCategory:0x007ff2134b69c0>

Thanks in advance for any and all help!

5
  • 1
    so you are defining the method and in the first line of the method you redefine the method to be an array? try renaming that variable. but that should no tbe yu problem for now. you try to access the method on a model instance, so it should be within the model. after that do a reload! in the console. Commented Apr 4, 2013 at 18:57
  • Wrote the last comment after your update. Let me try this as well. Commented Apr 4, 2013 at 19:01
  • Code is now in the Model, but NoMethodError persists. Commented Apr 4, 2013 at 19:05
  • You do a VenueCategory.new.hash_tags(:foo)? If so, does the error persist, when you exit the console and restart it? Commented Apr 4, 2013 at 19:07
  • Yes, method persists after restarting console and trying that command. Commented Apr 4, 2013 at 19:12

1 Answer 1

2

Helper modules are included in the scope of the view renderer, not the model. Change the view code to:

p = hash_tags(resource.venue_category)
Sign up to request clarification or add additional context in comments.

2 Comments

.. while the method is in the helper.
Nailed it. The method implementation apparently really sucks, but I can clean that up. Error is resolved - thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.