Rails 8.0.1 You are in good company

杰瑞发布于2025-12-31

Over the past two decades, Rails has taken countless companies to millions of users and billions in market valuations.

Welcome to Ruby on Rails. If you are looking to go from Hello World to IPO, you've come to just the right place. I'm gonna build a simple application that shows you the basic of Ruby on Rails and how you put it into production. Let's go.
We're gonna start by running 'rails new blog' to get the basic Rails skeleton, let's hop into that skeleton, and then we're gonna generate a scaffold for a post.
That post is just going to have a title that's string and a body that's a text, and as you can see here from what's being generated, we have everything that we need to set up a basic interface for that scaffold. There is a migration that'll set things up in the database. There is a controller, there are views, there's a model, there's even testing stubs and adjacent API on top. So let's run that migration, and as you can see here, we created the posts in the main schema file, and now, we're ready to have a look at the application that was generated here with the post scaffold.
So, let's jump into the post controller first. The controller is really what guides all the inbound actions you get into a Rails application, you'll have the user hitting (slash)/posts or /post/new, and it gets routed into the posts controller!
All post controllers follow the same convention. There are seven actions, you have index,show, new, edit, create and update and destroy! So there form a basic setup for configuring everything that's needed for a resource to be exposed to the web.
find it straight off an ID:
And if you scroll down here, you can see we have everything served in two flavors, we have both the HTML setup that'll render views directly, and then you have JSON that'll render for and an API. And as you can see here, we're also setting up a new post for some of those actions that require that, we're gonna find it straight off an ID passed in through the URL, and the post parameters are the ones we're using when we're creating and updating the application.
If we jump into the post model, you'll see there's actually nothing here. Everything in the post model is made available through introspection. So, a new post model will look at the schema for that table, and it will know that there is a title and there is a body, and we can access that title and that body directly through this post object.
And as it descends from application record, we can run everything from updates and destroys and what have you. This is also where we're gonna put our specific logic that's particular to this application beyond just the management of attributes.
And then finally, we have the views that are being generated. If we hop in here and have a look at the index view, you can see this is where we will list all the posts that are in the system.
And Rails uses ERB, which is essentially embedded Ruby. So, you mix HTML with Ruby in line, and you can split out more complicated functions into helper methods. But otherwise, this is the clearest cut setup in Rails, this is the default for integrating HTML and Ruby.
auxiliary police:
Now if we hop over and start up our development server, you do that with just bin/dev. If we were running a Rails application that also had auxiliary watcher processes such as one for ES build or for Tailwind, bin/dev would start those as well. But this version of our Rails blog, is just going to be built with all vanilla, no build swt up so we only need to start the Puma, Ruby web server,and we can hop over into the browser and see here.
This is the thing you're gonna see, when you start up a new Rails application, it'll tell you which version you're on, both for the ruby version, the Rails version, and the Rack version. That's running on localhost:3000 by default. But if we do slash posts here, you'll see the scaffold interface that we generated. Now, this is the index action, the one we just looked at in the view and from the controller. But if we click the New, you see here we have form for creating the new post with its title and its body. it's quiet basic, to put it mildly right now, but all the actions are mapped out. This scaffold interface is not meant for shipping into production, it is meant to showing you how to build a Rails application with the basics, and then you make it look pretty, you make it look nice.
variant:
But oh yeah! Let's show you real quick here,if you do a /post.json, you're gonna get that automatic API as well, as I showed you in the controller, there are two different paths, you have HTML, and you have JSON. You could also have added XML in there or another path, but by default, you just get these two different variants, the HTML and the JSON variant.
Now if we hop back into our editor here, I can add a little bit of styling to make this look slightly nicer than the very basic layout that you get with the scaffold.
compile:
By default, I like to use simple CSS. https://cdn.simplecss.org/simple.css. It is simply a individual file that's being included, I don't have to compile anything here, I'm just referencing it straight off their CDN. And if we save that and reload, you can see it just looks a little nicer.
Now , Rails has a bunch of different ways you can do the CSS, there's also a path where you can use Tailwind. Lots of people like that for good reason, and there are a bunch of different options, all the major CSS frameworks are available, but by default, we ship with a no build, as I said, intention and simple CSS just make things look prettier without having to adorn anything with classes, or what have you.
console instance variables:
Now, let me show you one of the first feature here. If we do raise exception inside the index action, "rails 'some exception' " you will see that Rails provides some really nice interface for dealing with that exception, seeing exactly where it happened. If I'm reloading here, you can see the line, it was raised on the source code that's around it, you can see a full trace. And down here, we even have a console! So, you can interact with the instance variables that have been set for this index actions, here's just at posts that's been made available.
And you can help diagnose whatever issue that it is that you have ! But let's remove that again and then let's look at the console from another angle. Now, you can get the console as I just showed you when an exception raised, but you can also get the console if you just run rails console.
Now you have access to your entire domain model. So if we find the first post that we created, we can update that post straight here from the console, if we hop back, you see, title is now changed from CLI. New command line interface ( CLI)
This is exceptionally helpful for interacting with your domain model, updating things on the fly, and as you will later, updating things even once you've deployed this to production!
Now, let's install something else here, let's install action_text, that is one of the frameworks that's part of Rails, but it's not set up by default, but you can set it up by running rails action_text:install, that's going to give you a WYSIWYG editor. That's currently powered by Trix! The open source, what you see is what you get editor made in JavaScript! And it also sets up active storage!
Active Storage is a way to deal with attachments and other files in your Rails application. When you run it through action_text:install, it'll automatically set up those active storage tables that we need, there is one for blob,and then we have one for text here.We run migrations to set that up again, and now that we've run action_text:install, it also added a couple of gems, so we need to restart our development server. I do that just by exiting out and just running the server again!
If we then hop into our post model, we can declare that post model has rich text body. We're gonna convert the plain text body that we had just a second ago to a rich text body that is accessible through the WYSIWYG editor, and that accepts those active storage attachments and uploads.
But before we can do that, let's change the text area we had in the form here for our new post to be a rich text area. "form.rich_textarea :body". That's basically all you have to change, and let's save that and hop back into creating a new post.
As you can see here, there is now a full WYSIWYG interface for creating the body. It comes with a default set of styles for the toolbar, you can change those, those styles are generated straight into your application, so you can make it look nice for yourself. Let's give some bold and italic text here, you see, that was all that was needed. but I think what's even nicer to look at here is if we do an upload and we add a file, you will see that file gets added with a preview directly to the WYSIWYG editor. And if we save that and we update the post, it is added to the post itself. And that then went through the whole process of doing a direct upload of the file when we dropped it into the editor, that uploads it straight to active storage. And then, we have access to that, and rendering it directly from whatever storage backend active storage is using. In this example , we're just storing on disk, but you could be storing your active storage in S3 or another object storage.