词汇:can
n. 罐头,一罐;金属容器
相关场景
- Now that we have that up, we can hop in and look it all up into the show action for the posts! That's gonna reference that common slot comments, that includes both the comments and the new form.>> Rails 8.0.1 You are in good company
- And then finally, let's paste in the form that we're gonna use. That form is going off a model, the new comment, but it's nested underneath the post, is that we automatically can deduce which URL that we should post this new form to.>> Rails 8.0.1 You are in good company
- It'll automatically know that the comment model should map to view slash comment slash comment, so it can look up the right partial file to use. And then below that, we have the form that we're referencing with the comments new. So, let's hop in and paste in the individual comment. As you can see here, we just give it a div, that has a dom ID so that we can reference it.>> Rails 8.0.1 You are in good company
- 16:03:
- Now, we're also gonna add a number of partials here. This is the templating system, basically, a sub-routine that you can refer to. There's gonna be three of them that includes the entire comment section. We're gonna reference that in our post show in just a second. And within that, we're gonna refer to another partial for an individual comment, and another partial again for the new setup. So, let's paste some of that in here, You can see this for the entire collection, it just has an H2 for the comments, and we render the post comments. This again uses Rails' convention over the configuration approach.>> Rails 8.0.1 You are in good company
- So let's actually also create the other direction of this association. You saw a comment belongs to a post, but then we're also gonna make the post has many comments. Now, we have a bidirectional association that we can work with in both ways.>> Rails 8.0.1 You are in good company
- Alright, if we hop into that comments controller, it was empty. As you can see there, I'm gonna pay something in that actually makes this stuff work! You'll see one principle of the controller setup we have is that we have these callbacks. Before action, we're gonna set posts. So before all the actions, we're going to reflect the fact that this is nested resource.>> Rails 8.0.1 You are in good company
- So, let's run the migration for that, that sets up the comments table. You can see here the schema that we've now built up. We've added a number of tables for action text and action storage. And then, we have added a comments table. That's what you can see here. As we had it in the migration where we were just referencing the post as a foreign key, and then we had the content as text.>> Rails 8.0.1 You are in good company
- If you look at something like hey.com, you'll see this technique in use on a major application, and you can view all of the Javascript that we use to build that application, and that's the default for Rails.>> Rails 8.0.1 You are in good company
- But we can also have a look at a specific file, you'll see it matches exactly what we have back there. That's not a development setup! That is what we're shipping in production. There is no minification, there's no transpilation, there's none of that nonsense because you just don't need it.>> Rails 8.0.1 You are in good company
- inspector:
- But really, what's unique here for Rails is the fact that we're using no build by default! So if I go over here in the inspector and look at the JavaScript files that are included,you can see we have the application js file with a little digest stamp on there. If we change anything that application js file, the digest is going to change, and the browser will redownload just the part!>> Rails 8.0.1 You are in good company
- zoo:
- And we will give it a format for what it should do with UTC timestamp, and turning it into a local time that we can have a look at.So if I reload here, you see it is November 13th, by the time of my recording at 3:28 PM in my local time zone, but actually underneath, the time tag is gonna be in UTC. That means we can cache this, and anyone around the world will still get the time displayed in their local time.>> Rails 8.0.1 You are in good company
- And we're gonna start local time here. And in the local time, we're gonna use it, and we're gonna use it for adding the updated at timestamp here. And as you can see here, we're just adding a time tag that's just a vanilla HTML tag that has a data local time that's what activates that local time JavaScript set up.>> Rails 8.0.1 You are in good company
- But now that we've add that, let's have a look at our application JS file.That's the default setup that you have that the scaffold is going to use. And as you can see, we're using turbo-rails, we're including all the stimulus controllers, if we have any.>> Rails 8.0.1 You are in good company
- So, we can do that using the import map pin command. And as you see, now that I hop back into our config import map, we've added the local text pin at the bottom, version 3.0.2. It pulled that straight off NPN, it downloaded that as a vendor dependency that we can check into our version control system. And now, we don't have any runtime dependency whatsoever on NPN, or anything else like that.>> Rails 8.0.1 You are in good company
- And then, there's the stimulus framework for creating that additional functionality that you might need in a really simple way. You can have a look at hotwire.dev to see more about that, but what we're gonna add here is a little piece of JavaScript to just add some additional functionality, pulling something in from pin.>> Rails 8.0.1 You are in good company
- 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.>> Rails 8.0.1 You are in good company
- 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.>> Rails 8.0.1 You are in good company
- 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.>> Rails 8.0.1 You are in good company
- 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!>> Rails 8.0.1 You are in good company
- 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)>> Rails 8.0.1 You are in good company
- 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.>> Rails 8.0.1 You are in good company
- 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.>> Rails 8.0.1 You are in good company
- 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.>> Rails 8.0.1 You are in good company
- 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.>> Rails 8.0.1 You are in good company
- 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.>> Rails 8.0.1 You are in good company