Ruby on Rails
Wednesday, August 7, 2019
To add-on a react-specific answer, you can also embed your data as JSON in the view (note that this is not limited to just react):
<%# Embed data for react to use as initial state %>
<script>window.reactProps = <%== @react_props.to_json %>;</script>
When you need to initialize your app, you can do:
// Spread reactProps
render(<Videos {...window.reactProps} />, document.getElementById('Videos'));
Developers may or may not want to use this flow because binding data to the window is sometimes considered poor form (global namespace pollution). In this case, the server already has the data we need in memory during the initial request, so why use another request? (plus it's good for SEO) Try looking at the page source for pinterest.com, it is a good example of a site that uses this flow to great effect.
Above all, remember that anything you pass to the browser is public!
On Wed, Aug 7, 2019 at 7:20 AM Walter Lee Davis <waltd@wdstudio.com> wrote:
On the JS side, the JavaScript application (or simple inline script, no difference) makes an HTTP request to or from the server, and the server (Rails) responds to that request, just as it would any other request. The Rails framework uses the "accept" headers sent as part of that request to determine what sort of response to send (JSON, inline JS, HTML, text, whatever else) and that's the long and short of that.
As far as databases go, ActiveRecord (the library, not the pattern) has connection adapters for each of the major database engines, and there are others you can plug in, say for Oracle or MS SQL Server, if you're not using Postgres or MySQL or similar. That's all handled at the gem + configuration level. Once you have plugged in the adapter for your specific engine, the actual communication between Rails (well more accurately, ActiveRecord) and the database engine is quite clever.
Ruby code like:
class Foo < ApplicationRecord
has_many :bars
end
class Bar < ApplicationRecord
belongs_to :foo
end
f = Foo.new
f.bars << Bar.first
f.save
...is first converted into an Arel AST (abstract syntax tree) that is database-independent, and then the database adapter is responsible for converting that AST into actual SQL for the specific database engine and sending and retrieving and interpreting data from the server. (Despite SQL being an ISO standard, no two implementations are identical.)
The upshot of this is that you can have (as I do at work) your tests running against SQLite, and your production server running Oracle 12. And your tests work and predict the results from your production server.
You can learn more about this at the Rails Guides https://guides.rubyonrails.org
Walter
> On Aug 7, 2019, at 12:20 AM, Noel <noelirias@gmail.com> wrote:
>
> Hello to All,
> I'm trying to understand something that I seem to get lost on, which is how exactly does Javascript communicate with Rails? I understand how to make it work, I can even make a React front-end and Rails API work together. But I feel like I am missing something fundamental when it comes to how this communication actually occurs. If you know if a resource that explains this, I would be grateful.
>
> Also, how does SQL communicate with Ruby or viceversa? A good resource that breaks it down would be great.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/dffe00e4-61d1-46a4-8123-34d631a6148b%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/EC75674A-CC23-4629-B6F5-5E089DF5E511%40wdstudio.com.
|
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CALn2xuBQqrr4PKd%3DEvOPZtvXxnL0OQ6QTVCRh0YsRmOEZGA3Mw%40mail.gmail.com.
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment