I have a Day object which contains some statistics and is updated daily. Here’s the two errors I’m getting:
undefined method `top_light_snown’ for #
undefined method `top_agents_snown’ for #
Depending on the contents of the view:
<h3 class='thin underline'>Daily Breakdown:</h3>
<%# debug @today %>
<%# @today.methods %>
Top User Agents Today: <%= @today.top_agents_snown %>
<br>
Top Keyword Triggers <%= @today.top_keyword_triggers %>
<br>
Top URLs Visited <%= @today.top_urls_visited %>
<br>
Top Injections Shown <%= @today.top_inj_shown %>
<br>
Top Popups Shown <%= @today.top_pop_shown %>
<br>
Top Custom Injections Shown <%= @today.top_cust_shown %>
<br>
Top Lightboxes Shown <%# @today.top_light_snown %>
<br>
Top Global Injections Shown <%= @today.top_glob_shown %>
Note that only .top_light_shown and .top_agents_shown cause errors. All the other methods work perfectly fine. Also note the two commented out debug commands. The result of debug @today shows that it does indeed have an attribute “top_light_shown,” it is also present in the list of methods from @today.methods, along with all the other ones used.
Here’s the contents of the controller – as you can see, I’m setting up the new @today Day object and pre-filling it with junk data to see if I could access it from the view. In this case, the if statement will always fail (but even if it doesn’t, I have the same undefined method errors)
if Day.find_by_id(Time.now.day)
stats_today = Day.find_by_id(Time.now.day)
@ads_today = stats_today.top_inj_shown.size + stats_today.top_pop_shown.size + stats_today.top_cust_shown.size + stats_today.top_light_shown.size + stats_today.top_glob_shown.size
@total_pageviews = Day.find_by_id(Time.now.day).pageviews
@today = Day.find_by_id(Time.now.day)
else
@today = Day.new(:top_agents_shown => ['Firefox', 'Chrome'], :top_light_shown => ['l-TEST', 'l-TEST2'])
puts @today.top_light_shown
puts @today.top_agents_shown
@ads_today = 'ERROR'
@total_pageviews = 'ERROR'
end
Here’s the server log after requesting the page with the puts working perfectly fine in the controller:
Started GET “/assets/favicons/favicon.ico” for 127.0.0.1 at 2012-08-01
19:13:03 -0600 Served asset /favicons/favicon.ico – 304 Not Modified (1ms)l-TEST l-TEST2 Firefox Chrome
Started GET “/dashboard” for 127.0.0.1 at 2012-08-01 19:16:38 -0600
Processing by DashboardController#index as HTML
Finally, here’s the contents of the Day.rb:
class Day
include MongoMapper::Document
key :number, Integer
key :pageviews, Integer
key :shown_injs, Integer
key :shown_pops, Integer
key :shown_custs, Integer
key :shown_lights, Integer
key :shown_globs, Integer
key :top_agents_shown, Array
key :top_keyword_triggers, Array
key :top_urls_visited, Array
key :top_inj_shown, Array
key :top_pop_shown, Array
key :top_cust_shown, Array
key :top_light_shown, Array
key :top_glob_shown, Array
end
What in the world is going on? Why are the IDENTICAL methods with a different name work and others do not? Why those particular two? Why do they work in the controller and not in the view?
Your view calls @today.top_agents_snown, but you define top_agents_shown. Just a misspelling. Some days are like that!