在 Javascript 中执行 ruby 方法 - Ruby on Rails App
Posted
技术标签:
【中文标题】在 Javascript 中执行 ruby 方法 - Ruby on Rails App【英文标题】:Execute ruby method within Javascript - Ruby on Rails App 【发布时间】:2016-12-05 23:24:57 【问题描述】:在 JWPlayer 上观看完视频后,我想在 UsersController 中执行一个 ruby 方法来更新用户模型属性
我不确定我做错了什么。
主页.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>
用户控制器
# 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 做
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
一切似乎都在工作,但它要求模板......我不想要模板,我希望它增加 current_user.workouts 1。 这是我从服务器日志中得到的...
ActionView::MissingTemplate(缺少模板用户/increase_workouts, 使用 :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]。搜索:* “/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: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' 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: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' 动作包 (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' 动作包 (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' 动作包(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' activesupport (4.2.2) lib/active_support/callbacks.rb:117:in
call' 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' 主动支持 (4.2.2) lib/active_support/notifications.rb:164:inblock 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:ininstrument' actionpack (4.2.2) lib/action_controller/metal/instrumentation.rb:30:in
process_action' 动作包(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' actionpack (4.2.2) lib/action_dispatch/journey/router.rb:43:in
block 在服务'动作包(4.2.2) lib/action_dispatch/journey/router.rb:30: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' 机架 (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' actionpack (4.2.2) lib/action_dispatch/middleware/flash.rb:260:in
call' 机架 (1.6.4) lib/rack/session/abstract/id.rb:225: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'活动记录(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' 动作包 (4.2.2) lib/action_dispatch/middleware/callbacks.rb:29:inblock 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: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' 网络控制台 (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' activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:in
tagged' activesupport (4.2.2) lib/active_support/tagged_logging.rb:68: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' 机架 (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' 机架 (1.6.4) lib/rack/lock.rb:17:incall' actionpack (4.2.2) lib/action_dispatch/middleware/static.rb:113:in
call' 机架 (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'渲染 /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.3 毫秒)
【问题讨论】:
这里没有足够的信息来真正回答这个问题,但您可以开始检查您的“完成”回调是否正在运行。只需在您的 ajax 请求之前粘贴console.log("complete callback");
即可。您可以通过.success
和.failure
回调检查响应(请参阅api.jquery.com/jquery.ajax)并查看rails 开发日志以查看您的请求是否正在发送。
您还想查看哪些其他类型的信息?
我知道“完成”回调正在运行,因为我设置了 alert("Hi!");在 ajax 之前,它工作得很好。
【参考方案1】:
了解您期望它做什么以及它实际在做什么会有所帮助。
日志告诉我们发生了异常:ActionView::MissingTemplate。 “缺少模板users/increase_workouts, application/increase_workouts
”消息告诉您有关缺少的视图模板的一些重要信息,它为您提供了它期望找到模板的位置。
当您通过发送请求与控制器操作交谈时,它需要知道如何响应您的请求。按照惯例,Rails 会渲染一个与动作同名的模板,例如index.html.erb
或index.jbuilder
,但在您的特殊操作的情况下,您要么需要创建响应模板,要么告诉控制器要render
。
在你在控制器动作中添加一个到锻炼之后,这样的事情应该会给你一个“成功”的响应。
render(json: message: "Workouts increased" , status: :ok) and return
但您也可以只使用render(status: :ok) and return
。这里的:ok
是一种人性化的方式来指示 HTTP 状态代码 200,您的客户端(您页面上的 JS)需要在响应中看到它。
更多详情请见http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
【讨论】:
我这样做了,它摆脱了缺失的模板。但它不会为锻炼增加 1。我不确定为什么会这样。这不是调用 #increase_workouts 方法吗?如果它在 current_user.workouts 中加 1,那不应该有效吗? 它在日志 Started GET "/users/increase_workouts" for 12.216.140.50 at 2016-08-01 17:32:19 +0000 Processing by UsersController#increase_workouts as / 用户负载 (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] 3ms 内完成 200 OK (Views: 0.2ms | ActiveRecord: 0.4ms) 想通了。我需要 .save 用户。 干得好!另请参阅 ActiveRecord 的update_attribute
。 ?以上是关于在 Javascript 中执行 ruby 方法 - Ruby on Rails App的主要内容,如果未能解决你的问题,请参考以下文章
Ruby on Rails - 如何在 application.js 中执行 require