ActionController :: UnknownFormat当使用respond_to do | format |时在rails 5 app

时间:2018-03-19 16:14:31

标签: javascript ruby-on-rails ruby actioncontroller respond-to

我正在尝试在profiles/show.html.erb的div中渲染页面以使其动态化。

我经常遇到这个错误。 ActionController::UnknownFormat

profiles_controller.rb

中的这一行
def show

   respond_to do |format|
        format.js { render :show_pages }
   end

end 

我安装了responders gem(根据http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#responders,有必要这样做。

在我的profiles/show.html.erb中只有这一行。

<div id="pages_content">


</div>

在我的views/profiles我有一个名为.js

show_pages.js.erb文件
$("#pages_content").html("<%=escape_javascript(render :partial=>"show_pages")%>");

而且在我的views/profiles中我有这个部分_show_pages.html.erb,这是应该在`中打开的部分。

**编辑 - 这是完整的痕迹**

    actionpack (5.1.5) lib/action_controller/metal/mime_responds.rb:203:in `respond_to'
app/controllers/profiles_controller.rb:8:in `show'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.5) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.5) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.5) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.5) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.5) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:844:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.5) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.5) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.5) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
railties (5.1.5) lib/rails/engine.rb:522:in `call'
puma (3.11.3) lib/puma/configuration.rb:225:in `call'
puma (3.11.3) lib/puma/server.rb:624:in `handle_request'
puma (3.11.3) lib/puma/server.rb:438:in `process_client'
puma (3.11.3) lib/puma/server.rb:302:in `block in run'
puma (3.11.3) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

任何人都可以告诉我为什么我收到此错误? 有什么我应该做的不同吗?

我对编码很新,所以我还在搞清楚。

感谢 CG

1 个答案:

答案 0 :(得分:0)

我认为您在format.js处使用了错误的用例。请阅读本文的使用方法:从文档"a simple example"开始。

如果您在format.html上方或下方添加format.js行,您的网页将会加载,错误就会消失。

您可以使用以下内容呈现部分服务器端:

<div id="pages_content">
  <%= render "show_pages %>
</div>

然后可以将自定义Javascript放在asset pipeline中,以包含更新客户端页面的代码。

如果您对预期用例进行一点扩展或有任何疑问,请尽快扩展。否则,希望现在增加一些清晰度:)