Over the past two decades, Rails has taken countless companies to millions of users and billions in market valuations.
杰瑞发布于2025-12-31
Over the past two decades, Rails has taken countless companies to millions of users and billions in market valuations.
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.- 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.- 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 , 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. 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! 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.