ActiveSupport::TimeWithZone to_s(:format) not working in view?

Hello, i got some strange behaviour here in my app i cannot work out.
Maybe here someone knows a soloution for this?
In the view:

<% @product.comments.each do |comment| %>

<%= comment.created_at.to_s(:db) %>

<% end %>

So the error i get is: wrong number of arguments (1 for 0) for the
to_s(:db)
When i ask the object comment.created_at.class what class it is, it
gives me: ActiveSupport::TimeWithZone
In the console i can take this object and use the to_s(:format) method
on it. Only in the view it is not working. Does anybody know a
solution to this?
I use Rails 3.0 here with MySQL as DB.

Thanks in advance,
Andreas

to_s formats with .strftime

On Sat, Sep 11, 2010 at 10:30 AM, Andreas Schmidt

sorry i meant to say that dates format with .strftime not to_s

Hello radhmes brito,

as you can see here:

it is possible to output a formatted like i tried. You can simply set
it up in /config/initializeres/time_formats.rb like this:
Time::DATE_FORMATS.merge!(
:mydate => ‘%d.%m.%Y’
)

Then you should be able to call to_s(:mydate) on and
ActiveSupport::TimeWitzZone Object. That works fine in the console,
but not in the view.

So you mentioned strftime to format the time. I tried that also :wink:
The only thing that changes then ist the error message:

undefined method `strftime’ for nil:NilClass

But how can this be a NilClass? The attribute is set in the database,
i checked that already via hand and if i ask the object for its class,
it gives me ActiveSupport::TimeWithZone back.

put this

comment.try(:created_at)

and tel me what happens

i want to see if it really is nil

On Sun, Sep 12, 2010 at 7:23 AM, Andreas Schmidt

i once used something like to_s(:mydate) but it never worked so i
always go
for the sure thing.

can you show the stack for both errors?

On Sat, Sep 11, 2010 at 3:37 PM, Andreas Schmidt

Hello radhmes brito,

here both traces for the errors.

For comment.created_at.strftime("%Y.%m.%d"):

undefined method strftime' for nil:NilClass activesupport (3.0.0) lib/active_support/whiny_nil.rb:48:inmethod_missing’
app/views/products/show.html.erb:64:in
_app_views_products_show_html_erb___1342911154_2182912460_0' activerecord (3.0.0) lib/active_record/associations/ association_collection.rb:430:inmethod_missing’
activerecord (3.0.0) lib/active_record/associations/
association_proxy.rb:216:in method_missing' activerecord (3.0.0) lib/active_record/associations/ association_proxy.rb:216:ineach’
activerecord (3.0.0) lib/active_record/associations/
association_proxy.rb:216:in send' activerecord (3.0.0) lib/active_record/associations/ association_proxy.rb:216:inmethod_missing’
activerecord (3.0.0) lib/active_record/associations/
association_collection.rb:430:in method_missing' app/views/products/show.html.erb:62:in_app_views_products_show_html_erb___1342911154_2182912460_0’
actionpack (3.0.0) lib/action_view/template.rb:135:in send' actionpack (3.0.0) lib/action_view/template.rb:135:inrender’
activesupport (3.0.0) lib/active_support/notifications.rb:54:in
instrument' actionpack (3.0.0) lib/action_view/template.rb:127:inrender’
actionpack (3.0.0) lib/action_view/render/rendering.rb:59:in
_render_template' activesupport (3.0.0) lib/active_support/notifications.rb:52:ininstrument’
activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb:
21:in instrument' activesupport (3.0.0) lib/active_support/notifications.rb:52:ininstrument’
actionpack (3.0.0) lib/action_view/render/rendering.rb:56:in
_render_template' actionpack (3.0.0) lib/action_view/render/rendering.rb:26:inrender’
actionpack (3.0.0) lib/abstract_controller/rendering.rb:114:in
_render_template' actionpack (3.0.0) lib/abstract_controller/rendering.rb:108:inrender_to_body’
actionpack (3.0.0) lib/action_controller/metal/renderers.rb:47:in
render_to_body' actionpack (3.0.0) lib/action_controller/metal/compatibility.rb:55:inrender_to_body’
actionpack (3.0.0) lib/abstract_controller/rendering.rb:101:in
render_to_string' actionpack (3.0.0) lib/abstract_controller/rendering.rb:92:inrender’
actionpack (3.0.0) lib/action_controller/metal/rendering.rb:17:in
render' actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb: 40:inrender_without_meta_tags’
activesupport (3.0.0) lib/active_support/core_ext/benchmark.rb:5:in
ms' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ 1.8/benchmark.rb:308:inrealtime’
activesupport (3.0.0) lib/active_support/core_ext/benchmark.rb:5:in
ms' actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb: 40:inrender_without_meta_tags’
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:
78:in cleanup_view_runtime' activerecord (3.0.0) lib/active_record/railties/controller_runtime.rb: 15:incleanup_view_runtime’
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:
39:in render_without_meta_tags' vendor/plugins/meta-tags/lib/meta_tags/controller_helper.rb:28:inrender’
actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb:
10:in default_render' actionpack (3.0.0) lib/action_controller/metal/mime_responds.rb:261:inretrieve_response_from_mimes’
actionpack (3.0.0) lib/action_controller/metal/mime_responds.rb:192:in
call' actionpack (3.0.0) lib/action_controller/metal/mime_responds.rb:192:inrespond_to’
app/controllers/products_controller.rb:20:in show' actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb:4:insend_action’
actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb:4:in
send_action' actionpack (3.0.0) lib/abstract_controller/base.rb:150:inprocess_action’
actionpack (3.0.0) lib/action_controller/metal/rendering.rb:11:in
process_action' actionpack (3.0.0) lib/abstract_controller/callbacks.rb:18:inprocess_action’
activesupport (3.0.0) lib/active_support/callbacks.rb:440:in
_run__1118320962__process_action__453433196__callbacks' activesupport (3.0.0) lib/active_support/callbacks.rb:409:insend’
activesupport (3.0.0) lib/active_support/callbacks.rb:409:in
_run_process_action_callbacks' activesupport (3.0.0) lib/active_support/callbacks.rb:93:insend’
activesupport (3.0.0) lib/active_support/callbacks.rb:93:in
run_callbacks' actionpack (3.0.0) lib/abstract_controller/callbacks.rb:17:inprocess_action’
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:
30:in process_action' activesupport (3.0.0) lib/active_support/notifications.rb:52:ininstrument’
activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb:
21:in instrument' activesupport (3.0.0) lib/active_support/notifications.rb:52:ininstrument’
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:
29:in process_action' actionpack (3.0.0) lib/action_controller/metal/rescue.rb:17:inprocess_action’
actionpack (3.0.0) lib/abstract_controller/base.rb:119:in process' actionpack (3.0.0) lib/abstract_controller/rendering.rb:40:inprocess’
actionpack (3.0.0) lib/action_controller/metal.rb:133:in dispatch' actionpack (3.0.0) lib/action_controller/metal/rack_delegation.rb: 14:indispatch’
actionpack (3.0.0) lib/action_controller/metal.rb:173:in action' actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:62:incall’
actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:62:in
dispatch' actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:27:incall’
rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in call' rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:inrecognize’
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:68:in
optimized_each' rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:inrecognize’
rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in call' actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:492:incall’
actionpack (3.0.0) lib/action_dispatch/middleware/
best_standards_support.rb:17:in call' actionpack (3.0.0) lib/action_dispatch/middleware/head.rb:14:incall’
rack (1.2.1) lib/rack/methodoverride.rb:24:in call' actionpack (3.0.0) lib/action_dispatch/middleware/params_parser.rb: 21:incall’
actionpack (3.0.0) lib/action_dispatch/middleware/flash.rb:182:in
call' actionpack (3.0.0) lib/action_dispatch/middleware/session/ abstract_store.rb:149:incall’
actionpack (3.0.0) lib/action_dispatch/middleware/cookies.rb:287:in
call' activerecord (3.0.0) lib/active_record/query_cache.rb:32:incall’
activerecord (3.0.0) lib/active_record/connection_adapters/abstract/
query_cache.rb:28:in cache' activerecord (3.0.0) lib/active_record/query_cache.rb:12:incache’
activerecord (3.0.0) lib/active_record/query_cache.rb:31:in call' activerecord (3.0.0) lib/active_record/connection_adapters/abstract/ connection_pool.rb:355:incall’
actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:46:in
call' activesupport (3.0.0) lib/active_support/callbacks.rb:415:in_run_call_callbacks’
actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:44:in
call' rack (1.2.1) lib/rack/sendfile.rb:107:incall’
actionpack (3.0.0) lib/action_dispatch/middleware/remote_ip.rb:48:in
call' actionpack (3.0.0) lib/action_dispatch/middleware/show_exceptions.rb: 46:incall’
railties (3.0.0) lib/rails/rack/logger.rb:13:in call' rack (1.2.1) lib/rack/runtime.rb:17:incall’
rack (1.2.1) lib/rack/lock.rb:11:in call' rack (1.2.1) lib/rack/lock.rb:11:insynchronize’
rack (1.2.1) lib/rack/lock.rb:11:in call' actionpack (3.0.0) lib/action_dispatch/middleware/static.rb:30:incall’
railties (3.0.0) lib/rails/application.rb:168:in call' railties (3.0.0) lib/rails/application.rb:77:insend’
railties (3.0.0) lib/rails/application.rb:77:in method_missing' passenger (2.2.15) lib/phusion_passenger/rack/request_handler.rb:92:inprocess_request’
passenger (2.2.15) lib/phusion_passenger/abstract_request_handler.rb:
207:in main_loop' passenger (2.2.15) lib/phusion_passenger/rack/application_spawner.rb: 120:inrun’
passenger (2.2.15) lib/phusion_passenger/rack/application_spawner.rb:
65:in spawn_application' passenger (2.2.15) lib/phusion_passenger/utils.rb:252:insafe_fork’
passenger (2.2.15) lib/phusion_passenger/rack/application_spawner.rb:
58:in spawn_application' passenger (2.2.15) lib/phusion_passenger/rack/application_spawner.rb: 41:inspawn_application’
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:150:in
spawn_application' passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:278:inhandle_spawn_application’
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:in
__send__' passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:inmain_loop’
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:196:in
`start_synchronously’
passenger (2.2.15) bin/passenger-spawn-server:61

For comment.created_at.to_s(:mydate):
wrong number of arguments (1 for 0)

app/views/products/show.html.erb:64:in to_s' app/views/products/show.html.erb:64:in_app_views_products_show_html_erb___1342911154_2182214980_0’
activerecord (3.0.0) lib/active_record/associations/
association_collection.rb:430:in method_missing' activerecord (3.0.0) lib/active_record/associations/ association_proxy.rb:216:inmethod_missing’
activerecord (3.0.0) lib/active_record/associations/
association_proxy.rb:216:in each' activerecord (3.0.0) lib/active_record/associations/ association_proxy.rb:216:insend’
activerecord (3.0.0) lib/active_record/associations/
association_proxy.rb:216:in method_missing' activerecord (3.0.0) lib/active_record/associations/ association_collection.rb:430:inmethod_missing’
app/views/products/show.html.erb:62:in
_app_views_products_show_html_erb___1342911154_2182214980_0' actionpack (3.0.0) lib/action_view/template.rb:135:insend’
actionpack (3.0.0) lib/action_view/template.rb:135:in render' activesupport (3.0.0) lib/active_support/notifications.rb:54:ininstrument’
actionpack (3.0.0) lib/action_view/template.rb:127:in render' actionpack (3.0.0) lib/action_view/render/rendering.rb:59:in_render_template’
activesupport (3.0.0) lib/active_support/notifications.rb:52:in
instrument' activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb: 21:ininstrument’
activesupport (3.0.0) lib/active_support/notifications.rb:52:in
instrument' actionpack (3.0.0) lib/action_view/render/rendering.rb:56:in_render_template’
actionpack (3.0.0) lib/action_view/render/rendering.rb:26:in render' actionpack (3.0.0) lib/abstract_controller/rendering.rb:114:in_render_template’
actionpack (3.0.0) lib/abstract_controller/rendering.rb:108:in
render_to_body' actionpack (3.0.0) lib/action_controller/metal/renderers.rb:47:inrender_to_body’
actionpack (3.0.0) lib/action_controller/metal/compatibility.rb:55:in
render_to_body' actionpack (3.0.0) lib/abstract_controller/rendering.rb:101:inrender_to_string’
actionpack (3.0.0) lib/abstract_controller/rendering.rb:92:in render' actionpack (3.0.0) lib/action_controller/metal/rendering.rb:17:inrender’
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:
40:in render_without_meta_tags' activesupport (3.0.0) lib/active_support/core_ext/benchmark.rb:5:inms’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
1.8/benchmark.rb:308:in realtime' activesupport (3.0.0) lib/active_support/core_ext/benchmark.rb:5:inms’
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:
40:in render_without_meta_tags' actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb: 78:incleanup_view_runtime’
activerecord (3.0.0) lib/active_record/railties/controller_runtime.rb:
15:in cleanup_view_runtime' actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb: 39:inrender_without_meta_tags’
vendor/plugins/meta-tags/lib/meta_tags/controller_helper.rb:28:in
render' actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb: 10:indefault_render’
actionpack (3.0.0) lib/action_controller/metal/mime_responds.rb:261:in
retrieve_response_from_mimes' actionpack (3.0.0) lib/action_controller/metal/mime_responds.rb:192:incall’
actionpack (3.0.0) lib/action_controller/metal/mime_responds.rb:192:in
respond_to' app/controllers/products_controller.rb:20:inshow’
actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb:4:in
send_action' actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb:4:insend_action’
actionpack (3.0.0) lib/abstract_controller/base.rb:150:in
process_action' actionpack (3.0.0) lib/action_controller/metal/rendering.rb:11:inprocess_action’
actionpack (3.0.0) lib/abstract_controller/callbacks.rb:18:in
process_action' activesupport (3.0.0) lib/active_support/callbacks.rb:440:in_run__1118320962__process_action__453433196__callbacks’
activesupport (3.0.0) lib/active_support/callbacks.rb:409:in send' activesupport (3.0.0) lib/active_support/callbacks.rb:409:in_run_process_action_callbacks’
activesupport (3.0.0) lib/active_support/callbacks.rb:93:in send' activesupport (3.0.0) lib/active_support/callbacks.rb:93:inrun_callbacks’
actionpack (3.0.0) lib/abstract_controller/callbacks.rb:17:in
process_action' actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb: 30:inprocess_action’
activesupport (3.0.0) lib/active_support/notifications.rb:52:in
instrument' activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb: 21:ininstrument’
activesupport (3.0.0) lib/active_support/notifications.rb:52:in
instrument' actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb: 29:inprocess_action’
actionpack (3.0.0) lib/action_controller/metal/rescue.rb:17:in
process_action' actionpack (3.0.0) lib/abstract_controller/base.rb:119:inprocess’
actionpack (3.0.0) lib/abstract_controller/rendering.rb:40:in
process' actionpack (3.0.0) lib/action_controller/metal.rb:133:indispatch’
actionpack (3.0.0) lib/action_controller/metal/rack_delegation.rb:
14:in dispatch' actionpack (3.0.0) lib/action_controller/metal.rb:173:inaction’
actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:62:in
call' actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:62:indispatch’
actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:27:in
call' rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:incall’
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in
recognize' rack-mount (0.6.13) lib/rack/mount/code_generation.rb:68:inoptimized_each’
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in
recognize' rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:incall’
actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:492:in
call' actionpack (3.0.0) lib/action_dispatch/middleware/ best_standards_support.rb:17:incall’
actionpack (3.0.0) lib/action_dispatch/middleware/head.rb:14:in call' rack (1.2.1) lib/rack/methodoverride.rb:24:incall’
actionpack (3.0.0) lib/action_dispatch/middleware/params_parser.rb:
21:in call' actionpack (3.0.0) lib/action_dispatch/middleware/flash.rb:182:incall’
actionpack (3.0.0) lib/action_dispatch/middleware/session/
abstract_store.rb:149:in call' actionpack (3.0.0) lib/action_dispatch/middleware/cookies.rb:287:incall’
activerecord (3.0.0) lib/active_record/query_cache.rb:32:in call' activerecord (3.0.0) lib/active_record/connection_adapters/abstract/ query_cache.rb:28:incache’
activerecord (3.0.0) lib/active_record/query_cache.rb:12:in cache' activerecord (3.0.0) lib/active_record/query_cache.rb:31:incall’
activerecord (3.0.0) lib/active_record/connection_adapters/abstract/
connection_pool.rb:355:in call' actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:46:incall’
activesupport (3.0.0) lib/active_support/callbacks.rb:415:in
_run_call_callbacks' actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:44:incall’
rack (1.2.1) lib/rack/sendfile.rb:107:in call' actionpack (3.0.0) lib/action_dispatch/middleware/remote_ip.rb:48:incall’
actionpack (3.0.0) lib/action_dispatch/middleware/show_exceptions.rb:
46:in call' railties (3.0.0) lib/rails/rack/logger.rb:13:incall’
rack (1.2.1) lib/rack/runtime.rb:17:in call' rack (1.2.1) lib/rack/lock.rb:11:incall’
rack (1.2.1) lib/rack/lock.rb:11:in synchronize' rack (1.2.1) lib/rack/lock.rb:11:incall’
actionpack (3.0.0) lib/action_dispatch/middleware/static.rb:30:in
call' railties (3.0.0) lib/rails/application.rb:168:incall’
railties (3.0.0) lib/rails/application.rb:77:in send' railties (3.0.0) lib/rails/application.rb:77:inmethod_missing’
passenger (2.2.15) lib/phusion_passenger/rack/request_handler.rb:92:in
process_request' passenger (2.2.15) lib/phusion_passenger/abstract_request_handler.rb: 207:inmain_loop’
passenger (2.2.15) lib/phusion_passenger/rack/application_spawner.rb:
120:in run' passenger (2.2.15) lib/phusion_passenger/rack/application_spawner.rb: 65:inspawn_application’
passenger (2.2.15) lib/phusion_passenger/utils.rb:252:in safe_fork' passenger (2.2.15) lib/phusion_passenger/rack/application_spawner.rb: 58:inspawn_application’
passenger (2.2.15) lib/phusion_passenger/rack/application_spawner.rb:
41:in spawn_application' passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:150:inspawn_application’
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:278:in
handle_spawn_application' passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:insend
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:in
main_loop' passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:196:instart_synchronously’
passenger (2.2.15) bin/passenger-spawn-server:61

Hope this helps?

so i did ut comment.try(:created_at) into the view, it outputs:
2010-09-11 03:18:46 +0200. So i think it cant be nil …

this is weird try using i18n to format it.
copy a file from github put it in the locale folder uncomment the set
locale
configuration then put an L function in front of it like this

l(comment.created_at)

On Sun, Sep 12, 2010 at 2:28 PM, Andreas Schmidt

uff , great it working

On Sun, Sep 12, 2010 at 3:05 PM, Andreas Schmidt

So i fixed it finally ! It was my fault …what else should have
happened :frowning:
In my controller i had a @product.comments.build. So in the each he
tried to
iterate over that object also … but it is empty.

Thanks for you help anyway!