-6ren">
gpt4 book ai didi

javascript - 在 Javascript 中执行 ruby​​ 方法 - Ruby on Rails 应用程序

转载 作者:行者123 更新时间:2023-11-27 22:41:55 25 4
gpt4 key购买 nike

在 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>

用户 Controller

# Adds video to number of workouts
def increase_workouts
if logged_in?
current_user.workouts += 1
else
redirect_to login_url
end
end

路线.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

一切似乎都正常,但它要求一个模板...我不需要模板,我希望它将 current_user.workouts 增加 1。这是我从服务器日志中得到的...

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'<br/>
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'<br/>
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:in
render_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:in
render_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:in
block 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:in
ms' 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:in
render'
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'<br/>
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: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:in run_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:in process_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:in block in instrument'<br/>
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:in
process_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:in
process' actionpack (4.2.2) lib/action_controller/metal.rb:196:in dispatch' actionpack (4.2.2)
lib/action_controller/metal/rack_delegation.rb:13:in
dispatch'
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:in
dispatch' actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:43:in serve'<br/>
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:in
serve'
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:in
call' actionpack (4.2.2) lib/action_dispatch/middleware/params_parser.rb:27:in call'<br/>
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:in
call'
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'<br/>
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'<br/>
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:in
call'
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:in
middleware_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:in
call' railties (4.2.2) lib/rails/rack/logger.rb:38:in call_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:in block in tagged'<br/>
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:in
call' actionpack (4.2.2) lib/action_dispatch/middleware/request_id.rb:21:in call' rack
(1.6.4) lib/rack/methodoverride.rb:22:in
call' 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:in
call' rack (1.6.4) lib/rack/sendfile.rb:113:in call' railties (4.2.2)
lib/rails/engine.rb:518:in
call' railties (4.2.2) lib/rails/application.rb:164:in call' rack (1.6.4)
lib/rack/content_length.rb:15:in
call' puma (3.1.0) lib/puma/configuration.rb:227:in call' puma (3.1.0)
lib/puma/server.rb:561:in
handle_request' puma (3.1.0) lib/puma/server.rb:406:in process_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)

最佳答案

这有助于了解您期望它做什么以及它实际在做什么。

日志告诉我们出现了异常:ActionView::MissingTemplate。 “缺少模板 users/increase_workouts, application/increase_workouts”消息告诉您有关缺少 View 模板的一些重要信息,并为您提供期望找到模板的位置。

当您通过发送请求与 Controller 操作对话时,它需要知道如何响应您的请求。按照惯例,Rails 会渲染一个与操作同名的模板,例如index.html.erbindex.jbuilder,但如果是特殊操作,您要么需要创建响应模板,要么告诉 Controller 要做什么渲染

在您的 Controller 操作中添加一个锻炼后,类似这样的事情应该会给您一个“成功”响应。


render(json: { message: "锻炼增加"}, status: :ok) 并返回

但您也可以只渲染(status: :ok)并返回。这里的 :ok 是一种人性化的指示 HTTP 状态代码 200 的方式,您的客户端(页面上的 JS)需要在响应中看到该状态代码。

更多详情,请参阅 http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

关于javascript - 在 Javascript 中执行 ruby​​ 方法 - Ruby on Rails 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38689390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com