GYTO
GYTO

Reputation: 500

React on Rails REST API vs Controller Props

I am trying to decide what is a better approach to manage Ruby on Rails and React in my application. I am using RoR 5 and gem react_on_rails which makes everything works fine for me. The question is if I should build a REST API with RoR and then access it through the React to control the behavior of the components that I need.

The problem that I found and I could not find a solution at now is that I cannot put props from the controller in which I have two different props. For example,

class FooBarsController < ApplicationController
  def index
    @product_props = Product.all
    @testimonial_props = Testimonial.all.where('featured', [true])
  end
end

This is what I am doing in the view

<%= react_compontent('FooBarProduct', props: @product_props) %>
<h2>Title</h2>
<%= react_compontent('FooBarTestimonial', props: @testimonial_props) %>

I know that the logic of the controller should not manage two variables at once. it means that I cannot put my two tables in one place and I have to handle the business logic on the model.

In this case what would the best way to handle React props through REST API or Controller props even if I overloading the controller?

Upvotes: 0

Views: 127

Answers (1)

CodeFromthe510
CodeFromthe510

Reputation: 556

IMO, you should use a serializer on your controllers and attach the global variable data to the serializer response. this will prevent a ton of extranneous values from escaping into the client.

If you must keep your API pure and restful, make a Node app with CRA and have that talk to your rails 5 API and make 2 calls via componentDidMount() on your react component.

Upvotes: 1

Related Questions