0

我已经建立了一个使用 Turbo 的本地 Rails 应用程序。我正在尝试填充Turbo Demo iOS 应用程序以显示我的本地 Rails 应用程序。有没有人能够做类似的事情?

我切换Demo.current到使用本地地址,但所有请求都显示“错误加载页面”屏幕,演示应用程序中没有有用的日志。

Rails 应用程序将我的基本路线显示为正在尝试,并具有某种形式的持久性并重试401错误。我将SceneController默认 URL 更改为加载 a /networks/all-people,这需要授权用户(通过设计)希望看到身份验证逻辑将如何进行。下面是运行 Demo 应用程序时的 Rails 输出:

Started GET "/networks/all-people" for 127.0.0.1 at 2021-01-21 19:50:26 -0500
Processing by NetworksController#all_people as HTML
Completed 401 Unauthorized in 0ms (Allocations: 256)


Started GET "/users/sign_in" for 127.0.0.1 at 2021-01-21 19:50:26 -0500
Processing by Devise::SessionsController#new as HTML
  Rendering layout layouts/application.html.erb
  Rendering devise/sessions/new.html.erb within layouts/application
  Rendered devise/shared/_links.html.erb (Duration: 0.3ms | Allocations: 218)
  Rendered devise/sessions/new.html.erb within layouts/application (Duration: 1.7ms | Allocations: 1056)
  Rendered layout layouts/application.html.erb (Duration: 9.4ms | Allocations: 7318)
Completed 200 OK in 10ms (Views: 9.8ms | Allocations: 7945)

有没有人能够让 Demo 应用程序与本地 Rails 服务器一起工作?我完全不确定这里发生了什么错误,或者在这里匀场是否是一个好主意。

Turbo 正在加载,正如从我的浏览器访问时触发的以下事件侦听器所验证的那样。

<script>
  document.addEventListener("turbo:load", function(e) {
    console.log("TURBO LOADED");
  });
</script>
4

1 回答 1

1

要让 Devise 与 Turbo 一起工作,需要做一些事情。

添加app/controllers/turbo_controller

# frozen_string_literal: true

class TurboController < ApplicationController
  class Responder < ActionController::Responder
    def to_turbo_stream
      controller.render(options.merge(formats: :html))
    rescue ActionView::MissingTemplate => e
      raise e if get?

      if has_errors? && default_action
        render rendering_options.merge(formats: :html, status: :unprocessable_entity)
      else
        redirect_to navigation_location
      end
    end
  end

  self.responder = Responder
  respond_to :html, :turbo_stream
end

添加data: { turbo: "false" }到您的设计登录表单

# app/views/devise/sessions/new.html.erb
<%= form_for(resource, as: resource_name, url: session_path(resource_name), data: { turbo: "false" }) do

更新config/initializers/devise.rb

class TurboFailureApp < Devise::FailureApp
  def skip_format?
    %w[html turbo_stream */*].include?(request_format.to_s)
  end
end

config.parent_controller = "TurboController"

config.navigational_formats = ["*/*", :html, :turbo_stream]

config.warden do |manager|
  manager.failure_app = TurboFailureApp
end

感谢GoRails的详细解释!

于 2021-01-22T03:26:53.617 回答