1
votes

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'
actionview (4.2.2) lib/action_view/lookup_context.rb:121:in
find'
actionview (4.2.2) lib/action_view/renderer/abstract_renderer.rb:18:in find_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:in render'
actionview (4.2.2) lib/action_view/renderer/renderer.rb:42:in
render_template' actionview (4.2.2) lib/action_view/renderer/renderer.rb:23:in render' 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:inrender_to_body' actionpack (4.2.2) lib/action_controller/metal/rendering.rb:32:in render_to_body' actionpack (4.2.2) lib/action_controller/metal/renderers.rb:37:inrender_to_body'
actionpack (4.2.2) lib/abstract_controller/rendering.rb:25:in render' actionpack (4.2.2) lib/action_controller/metal/rendering.rb:16:in render' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:44:in block (2 levels) in render' activesupport (4.2.2) lib/active_support/core_ext/benchmark.rb:12:inblock in ms'
/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/benchmark.rb:308:in realtime' activesupport (4.2.2) lib/active_support/core_ext/benchmark.rb:12:inms' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:44:in block 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:in cleanup_view_runtime' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:43:inrender'
actionpack (4.2.2) lib/action_controller/metal/implicit_render.rb:10:in default_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:in process_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:in block in process_action'
activesupport (4.2.2) lib/active_support/callbacks.rb:117:in
call'
activesupport (4.2.2) lib/active_support/callbacks.rb:555:in block (2 levels) in compile' activesupport (4.2.2) lib/active_support/callbacks.rb:505:incall' 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:in run_callbacks' actionpack (4.2.2) lib/abstract_controller/callbacks.rb:19:inprocess_action'
actionpack (4.2.2) lib/action_controller/metal/rescue.rb:29:in process_action' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:32:inblock in process_action' activesupport (4.2.2) lib/active_support/notifications.rb:164:in block in instrument'
activesupport (4.2.2) lib/active_support/notifications/instrumenter.rb:20:in
instrument'
activesupport (4.2.2) lib/active_support/notifications.rb:164:in instrument' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:30:inprocess_action' actionpack (4.2.2) lib/action_controller/metal/params_wrapper.rb:250:in process_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:in process' actionview (4.2.2) lib/action_view/rendering.rb:30:inprocess' actionpack (4.2.2) lib/action_controller/metal.rb:196:in dispatch' actionpack (4.2.2) lib/action_controller/metal/rack_delegation.rb:13:indispatch'
actionpack (4.2.2) lib/action_controller/metal.rb:237:in block in action' actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:74:indispatch' actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:43:in serve'
actionpack (4.2.2) lib/action_dispatch/journey/router.rb:43:in
block in serve' actionpack (4.2.2) lib/action_dispatch/journey/router.rb:30:in each' actionpack (4.2.2) lib/action_dispatch/journey/router.rb:30:inserve'
actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:819:in call' rack (1.6.4) lib/rack/etag.rb:24:incall' rack (1.6.4) lib/rack/conditionalget.rb:25:in call' rack (1.6.4) lib/rack/head.rb:13:incall' actionpack (4.2.2) lib/action_dispatch/middleware/params_parser.rb:27:in call'
actionpack (4.2.2) lib/action_dispatch/middleware/flash.rb:260:in
call' rack (1.6.4) lib/rack/session/abstract/id.rb:225:in context' rack (1.6.4) lib/rack/session/abstract/id.rb:220:incall'
actionpack (4.2.2) lib/action_dispatch/middleware/cookies.rb:560:in call' 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:in call' activerecord (4.2.2) lib/active_record/migration.rb:378:in call' actionpack (4.2.2) lib/action_dispatch/middleware/callbacks.rb:29:in block in call'
activesupport (4.2.2) lib/active_support/callbacks.rb:88:in
_run_callbacks' activesupport (4.2.2) lib/active_support/callbacks.rb:776:in _run_call_callbacks'
activesupport (4.2.2) lib/active_support/callbacks.rb:81:in
run_callbacks' actionpack (4.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in call' actionpack (4.2.2) lib/action_dispatch/middleware/reloader.rb:73:incall'
actionpack (4.2.2) lib/action_dispatch/middleware/remote_ip.rb:78:in call' web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:18:inmiddleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in call' actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:incall' railties (4.2.2) lib/rails/rack/logger.rb:38:in call_app' railties (4.2.2) lib/rails/rack/logger.rb:20:inblock in call' activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in block in tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:in
tagged' activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in tagged' railties (4.2.2) lib/rails/rack/logger.rb:20:incall' actionpack (4.2.2) lib/action_dispatch/middleware/request_id.rb:21:in call' rack (1.6.4) lib/rack/methodoverride.rb:22:incall' rack (1.6.4) lib/rack/runtime.rb:18:in call' 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:in call' actionpack (4.2.2) lib/action_dispatch/middleware/static.rb:113:incall' rack (1.6.4) lib/rack/sendfile.rb:113:in call' railties (4.2.2) lib/rails/engine.rb:518:incall' railties (4.2.2) lib/rails/application.rb:164:in call' rack (1.6.4) lib/rack/content_length.rb:15:incall' puma (3.1.0) lib/puma/configuration.rb:227:in call' puma (3.1.0) lib/puma/server.rb:561:inhandle_request' puma (3.1.0) lib/puma/server.rb:406:in process_client' puma (3.1.0) lib/puma/server.rb:271:inblock 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)

1
There's not enough information here to really answer the question, but you can start by checking to see your "complete" callback is running. Just stick a 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
What other type of information would you like to see?Doughtz
I know the "complete" callback is running because I have put alert("Hi!"); before ajax and it worked just fine.Doughtz

1 Answers

1
votes

It helps to know what you expect it to do and what it is actually doing.

The logs tell us there is an exception being raised: ActionView::MissingTemplate. The "Missing template users/increase_workouts, application/increase_workouts" message is telling you something important about a missing view template and it's giving you the locations where it expects to find a template.

When you talk to a controller action by sending a request, it needs to know how to respond to your request. By convention, Rails renders a template with the same name as the action, e.g. index.html.erb or index.jbuilder, but in the case of your special action, you either need to create a response template or tell the controller what to render.

Something like this right after you add one to the workouts in your controller action should give you a "success" response.

render(json: { message: "Workouts increased" }, status: :ok) and return

But you can also just render(status: :ok) and return. The :ok here is a human-friendly way of indicating the HTTP status code 200, which your client (the JS on your page) needs to see in a response.

For more details, see http://guides.rubyonrails.org/layouts_and_rendering.html#using-render