Upon completing watching a video on JWPlayer, I want to execute a ruby method in the UsersController that updates a User model attribute
I am not sure what I am doing wrong.
Home.html.erb
<script src="//content.jwplatform.com/players/<%= a = Video.mediakey(Time.now.strftime("%Y%m%d").to_i) %>-3sPMNLmx.js"></script>
<script>
jwplayer().on('complete', function() {
$.ajax("/users/increase_workouts")
});
</script>
UsersController
# Adds video to number of workouts
def increase_workouts
if logged_in?
current_user.workouts += 1
else
redirect_to login_url
end
end
Routes.rb
Rails.application.routes.draw do
get 'password_resets/new' get 'password_resets/edit' get 'sessions/new' get 'users/new' get 'videos/new' get 'videos/index' root "static_pages#home" get 'static_pages/about' get 'static_pages/contact' get 'static_pages/help' get 'signup' => 'users#new' get 'upload' => 'videos#new' get 'login' => 'sessions#new' post 'login' => 'sessions#create' delete 'logout' => 'sessions#destroy' resources :users do get :increase_workouts, on: :collection end resources :videos resources :account_activations, only: [:edit] resources :password_resets, only: [:new, :create, :edit, :update] end
Everything seems to be working, but it is asking for a template... I don't want a template, I want it to increase current_user.workouts by 1. This is what I get from the server log...
ActionView::MissingTemplate (Missing template users/increase_workouts, application/increase_workouts with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates" * "/home/ubuntu/workspace/app/views" * "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/app/views" ): actionview (4.2.2) lib/action_view/path_set.rb:46:in
find'
find'
actionview (4.2.2) lib/action_view/lookup_context.rb:121:in
actionview (4.2.2) lib/action_view/renderer/abstract_renderer.rb:18:infind_template' actionview (4.2.2) lib/action_view/renderer/template_renderer.rb:40:in
determine_template' actionview (4.2.2) lib/action_view/renderer/template_renderer.rb:8:inrender'
render_template' actionview (4.2.2) lib/action_view/renderer/renderer.rb:23:in
actionview (4.2.2) lib/action_view/renderer/renderer.rb:42:inrender' actionview (4.2.2) lib/action_view/rendering.rb:100:in
_render_template'
actionpack (4.2.2) lib/action_controller/metal/streaming.rb:217:in_render_template' actionview (4.2.2) lib/action_view/rendering.rb:83:in
render_to_body' actionpack (4.2.2) lib/action_controller/metal/rendering.rb:32:inrender_to_body' actionpack (4.2.2) lib/action_controller/metal/renderers.rb:37:in
render_to_body'
actionpack (4.2.2) lib/abstract_controller/rendering.rb:25:inrender' actionpack (4.2.2) lib/action_controller/metal/rendering.rb:16:in
render' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:44:inblock (2 levels) in render' activesupport (4.2.2) lib/active_support/core_ext/benchmark.rb:12:in
block in ms'
/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/benchmark.rb:308:inrealtime' activesupport (4.2.2) lib/active_support/core_ext/benchmark.rb:12:in
ms' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:44:inblock in render' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:87:in
cleanup_view_runtime' activerecord (4.2.2) lib/active_record/railties/controller_runtime.rb:25:incleanup_view_runtime' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:43:in
render'
actionpack (4.2.2) lib/action_controller/metal/implicit_render.rb:10:indefault_render' actionpack (4.2.2) lib/action_controller/metal/implicit_render.rb:5:in
send_action' actionpack (4.2.2) lib/abstract_controller/base.rb:198:inprocess_action' actionpack (4.2.2) lib/action_controller/metal/rendering.rb:10:in
process_action' actionpack (4.2.2) lib/abstract_controller/callbacks.rb:20:inblock in process_action'
call'
activesupport (4.2.2) lib/active_support/callbacks.rb:117:in
activesupport (4.2.2) lib/active_support/callbacks.rb:555:inblock (2 levels) in compile' activesupport (4.2.2) lib/active_support/callbacks.rb:505:in
call' activesupport (4.2.2) lib/active_support/callbacks.rb:92:in_run_callbacks' activesupport (4.2.2) lib/active_support/callbacks.rb:776:in
_run_process_action_callbacks' activesupport (4.2.2) lib/active_support/callbacks.rb:81:inrun_callbacks' actionpack (4.2.2) lib/abstract_controller/callbacks.rb:19:in
process_action'
actionpack (4.2.2) lib/action_controller/metal/rescue.rb:29:inprocess_action' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:32:in
block in process_action' activesupport (4.2.2) lib/active_support/notifications.rb:164:inblock in instrument'
instrument'
activesupport (4.2.2) lib/active_support/notifications/instrumenter.rb:20:in
activesupport (4.2.2) lib/active_support/notifications.rb:164:ininstrument' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:30:in
process_action' actionpack (4.2.2) lib/action_controller/metal/params_wrapper.rb:250:inprocess_action' activerecord (4.2.2) lib/active_record/railties/controller_runtime.rb:18:in
process_action' actionpack (4.2.2) lib/abstract_controller/base.rb:137:inprocess' actionview (4.2.2) lib/action_view/rendering.rb:30:in
process' actionpack (4.2.2) lib/action_controller/metal.rb:196:indispatch' actionpack (4.2.2) lib/action_controller/metal/rack_delegation.rb:13:in
dispatch'
actionpack (4.2.2) lib/action_controller/metal.rb:237:inblock in action' actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:74:in
dispatch' actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:43:inserve'
block in serve' actionpack (4.2.2) lib/action_dispatch/journey/router.rb:30:in
actionpack (4.2.2) lib/action_dispatch/journey/router.rb:43:ineach' actionpack (4.2.2) lib/action_dispatch/journey/router.rb:30:in
serve'
actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:819:incall' rack (1.6.4) lib/rack/etag.rb:24:in
call' rack (1.6.4) lib/rack/conditionalget.rb:25:incall' rack (1.6.4) lib/rack/head.rb:13:in
call' actionpack (4.2.2) lib/action_dispatch/middleware/params_parser.rb:27:incall'
call' rack (1.6.4) lib/rack/session/abstract/id.rb:225:in
actionpack (4.2.2) lib/action_dispatch/middleware/flash.rb:260:incontext' rack (1.6.4) lib/rack/session/abstract/id.rb:220:in
call'
actionpack (4.2.2) lib/action_dispatch/middleware/cookies.rb:560:incall' activerecord (4.2.2) lib/active_record/query_cache.rb:36:in
call' activerecord (4.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:incall' activerecord (4.2.2) lib/active_record/migration.rb:378:in
call' actionpack (4.2.2) lib/action_dispatch/middleware/callbacks.rb:29:inblock in call'
_run_callbacks' activesupport (4.2.2) lib/active_support/callbacks.rb:776:in
activesupport (4.2.2) lib/active_support/callbacks.rb:88:in_run_call_callbacks'
run_callbacks' actionpack (4.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in
activesupport (4.2.2) lib/active_support/callbacks.rb:81:incall' actionpack (4.2.2) lib/action_dispatch/middleware/reloader.rb:73:in
call'
actionpack (4.2.2) lib/action_dispatch/middleware/remote_ip.rb:78:incall' web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:18:in
middleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:incall' actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in
call' railties (4.2.2) lib/rails/rack/logger.rb:38:incall_app' railties (4.2.2) lib/rails/rack/logger.rb:20:in
block in call' activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:inblock in tagged'
tagged' activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in
activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:intagged' railties (4.2.2) lib/rails/rack/logger.rb:20:in
call' actionpack (4.2.2) lib/action_dispatch/middleware/request_id.rb:21:incall' rack (1.6.4) lib/rack/methodoverride.rb:22:in
call' rack (1.6.4) lib/rack/runtime.rb:18:incall' activesupport (4.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in
call' rack (1.6.4) lib/rack/lock.rb:17:incall' actionpack (4.2.2) lib/action_dispatch/middleware/static.rb:113:in
call' rack (1.6.4) lib/rack/sendfile.rb:113:incall' railties (4.2.2) lib/rails/engine.rb:518:in
call' railties (4.2.2) lib/rails/application.rb:164:incall' rack (1.6.4) lib/rack/content_length.rb:15:in
call' puma (3.1.0) lib/puma/configuration.rb:227:incall' puma (3.1.0) lib/puma/server.rb:561:in
handle_request' puma (3.1.0) lib/puma/server.rb:406:inprocess_client' puma (3.1.0) lib/puma/server.rb:271:in
block in run' puma (3.1.0) lib/puma/thread_pool.rb:111:in `block in spawn_thread'Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/missing_template.text.erb (1.3ms)
console.log("complete callback");
before your ajax request. You can inspect the response via the.success
and.failure
callbacks (see api.jquery.com/jquery.ajax) and look at the rails development logs to see if your request is being sent. – Allolex