Skip to content

Instantly share code, notes, and snippets.

@English3000
Created December 2, 2017 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save English3000/be95a4102e764d2be89d652ff715c2ec to your computer and use it in GitHub Desktop.
Save English3000/be95a4102e764d2be89d652ff715c2ec to your computer and use it in GitHub Desktop.

JavaScript sends the request from the client to the server. A request can be sent simply by clicking on a button. Research JavaScript for yourself to discover how; but that's one main thing it is for.

So your request goes to the router, which directs it to a controller. Welcome to the backend!

App Academy teaches Ruby--

a server-side scripting language, or a coding language you use to write scripts--text files with .rb extensions--for your backend server of your website you've coded

--on Rails--

a backend/server-side framework written in Ruby which handles routed AJAX requests.

The Controller

The controller receives the payload from the router. The controller can do a number of different things with the payload. What is done depends on what kind of request has been made (the data for that, which tells the controller what to do, is a part of the payload). Whatever the controller does is an action. Yes, that's the technical term too!

So the controller does a specified action. The action involves a model. What's a model? A model is a noun--a user, a post, a conversation. A model has attributes, e.g. a user has a username and password. These attributes are stored in a database (such as PostgreSQL).

The controller action is just a method (or function, or--as aforementioned--action) which creates or does something to an existing model from the database. Literally, a model is a row in the database and its columns are its attributes. So a table in a database keeps track of the different instances of the model. These different instances all share a class, e.g. a User class. And Rails has some built-in methods for each model/class that you write. So you can call User.new to create a new user in your action method. You would then store this new user as an instance variable, @user (or whatever name you choose, although in coding, straightforward names are very helpful).

You would then use another Rails method on that instance variable, @user.save, to create a new row of data in your database. Or that's one action you can take. It's called create. It is a POST request which sends data to record as a row in the database. So, for example, when you sign up for an account on a website, you submit a POST request to create that account and your details are saved and stored in the database. Whenever you from that point forward sign into your account, your details are checked against the database. That's how the site protects your account from strangers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment