简体   繁体   English

我应该如何将 React 前端添加到使用 Rails 构建的 Web 应用程序中?

[英]How should I approach adding a react front-end to a web app built with rails?

I have an app that I've built with ruby on rails in the backend with the front end being simple rails views.我有一个应用程序,我在后端用 ruby​​ on rails 构建了一个应用程序,前端是简单的 rails 视图。 I'd like to add react to the front end to make it look nicer.我想在前端添加反应以使其看起来更好。 I have read about using the react-rails gem, or creating a new react app and using the rails app as API only, so I'm wondering what the best approach is.我已经阅读了有关使用 react-rails gem 或创建新的 react 应用程序并仅将 rails 应用程序用作 API 的内容,所以我想知道最好的方法是什么。 I also used the devise ruby gem for handling user auth-- but I have seen some things that suggest I need to use the devise_token_auth gem if I want to go the rails API route... How would you approach adding a react front end to a fully built rails app?我还使用 devise ruby​​ gem 来处理用户身份验证——但是我看到一些事情表明如果我想使用 rails API 路线,我需要使用 devise_token_auth gem ......你会如何处理添加反应前端到一个完全构建的 Rails 应用程序?

Ive done both styles that you mentioned, in my own experience i liked more having a separated react project and using a rails api maybe because i use docker for my builds and had several problems when i used the rails-react gem and webpacker but the advantage is that you wont have to do 2 deploys, in both approaches you would have to setup APIs.我已经完成了你提到的两种风格,根据我自己的经验,我更喜欢有一个单独的反应项目和使用 rails api 可能是因为我使用 docker 进行我的构建并且在我使用 rails-react gem 和 webpacker 时遇到了一些问题,但优点是是您不必进行 2 次部署,在这两种方法中您都必须设置 API。

ps : webpacker precompiles in rails can get pretty heavy sometimes ps:webpacker 在 rails 中的预编译有时会变得非常繁重

For both you will need a devise JWT token for the authentication tho, here is a nice tutorial on how to set it up对于两者,您都需要一个用于身份验证的设计 JWT 令牌, 是一个关于如何设置它的不错的教程

if you want to have both auth methods (html & json) you can use something like this in your sessions controller如果您想同时拥有两种身份验证方法(html 和 json),您可以在会话控制器中使用类似的东西

    class Users::SessionsController < Devise::SessionsController
  respond_to :json, :html
  # before_action :configure_sign_in_params, only: [:create]

  def create
    if request.format.html?
      super
      # super {
      #   cookies[:token] = current_token || ""
      # }
    else
      resource = User.find_for_database_authentication(email: params[:email])
      return invalid_login_attempt unless resource

      if resource.valid_password?(params[:password])
        sign_in(resource, store: false)
        @payload = [resource, current_token]
        render(status: 200, success: true, json: @payload)
      else
        invalid_login_attempt
      end
    end
  end

  def invalid_login_attempt
    warden.custom_failure!
    render json: {
      success: false,
      message: 'Error with your email or password'
    }, status: 401, status: :unauthorized
  end

  def current_token
    request.env['warden-jwt_auth.token']
  end

  private

  def respond_with(resource, _opts = {})
    if request.format.html?
      super
    else
      render status: 401, success: false, json: { error: 'No autorizado' }
    end
  end

  def respond_to_on_destroy
    if request.format.html?
      super
    else
      head :ok
    end
  end
end

there is probably a best way to do this but works for me lol可能有最好的方法来做到这一点,但对我有用,哈哈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该在后端(Rails API)还是前端(React/Redux)上查询和过滤 - Should I query and filter on the back-end (Rails API) or front-end (React/Redux) 如何使用 Rails 路由从 React 前端重定向? - How to redirect from React front-end with Rails routes? Android应用程式如何搭配Java前端和Rails 4后端? - How Android app with Java front-end and Rails 4 backend? Web应用程序的Ruby前端和PHP后端? - Ruby front-end and PHP back-end for a web app? Capybara 测试不起作用,而实际应用程序运行良好。 Rails 5 中的 React 前端 - Capybara tests don't work, while actual app works fine. React front-end in Rails 5 使用 Rails 6 和 React 作为前端的 500 内部服务器错误 - 500 Internal Server Error with Rails 6 and React as front-end 如何在Rails中编写用于Markdown编辑的前端界面? - How to write a front-end interface for Markdown editing in Rails? 如何为Shopify应用程序增加安全性? (webhooks和前端) - How can I add security to my Shopify App? (webhooks & front-end) 如何使用 Vue 前端和 rails 后端实现 env vairables? 是否需要使用 dotenv package 来完成此任务? - How do I implement env vairables with a Vue front-end and a rails backe-end? Is it necissary to use the dotenv package to complete this task? 通过前端将Feed添加到Feedjira - Adding Feeds to Feedjira via Front-End
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM