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.

  • skeleton n. 骨架,骨骼;纲要;骨瘦如柴的人
  • scaffold n. 脚手架;绞刑台;鹰架
  • auxiliary n. 助动词;辅助者,辅助物;附属机构
  • digest vt. 消化;吸收;融会贯通
  • slot n. 狭槽;水沟;硬币投币口;位置
  • hub n. 中心;毂;木片
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.
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.
We are pacing in the comment, and we're using that same time tag as we were using with the post, but this time, we are going to use time ago, so we get that nice two minutes ago on when something went posted rather than a local time spelled out with AM PM set up.
time_tag comment.update_at, "data-local": "time-ago"
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.
form.text_area :content, size: "20x5"
And your comment is just be a text area for content. We could have made this a rich text field as well, but let's keep things simple and just keep it in plain text!
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.
All right, let's save that and hop back into our browser. Oop! I made a mistake here! When we generated the resource,it added a route for the new comments,but that route was not nested by default. We actually need to go into our routes.rb, that resource we added needs to be nested.
When it is nested, we get the fact that it's gonna be slash post slash on slash comments, and we have the association is set up nicely. Now, let's reload! Now it works, we have our comments field underneath. we can add the first comment. And as you can see here, this is my first comment a second ago that was the local time doing its time ago conversion.
当它被嵌套时,我们得到的事实是,它将是斜线后斜线对斜线注释,并且我们已经很好地建立了关联。现在,让我们重新加载!现在它工作了,我们在下面有我们的评论字段。我们可以添加第一条评论。正如你在这里看到的,这是我一秒钟前的第一条评论,当时当地时间正在进行时间转换。
Now, let's set things up to be dynamic, such that when we add a new comment to one of these, it's going to update the other as well. This is how we use web sockets in Rails using action cable, one of the frameworks that we have to create updates that are distributed automatically without folks having to reload their browser.
So if we scroll down to the bottom here, we are ready. The first thing we're gonna do, we're gonna add a turbo stream from post to the show files to the show template.That's gonna set up the web socket connection and subscribe us to a channel named after that particular post that's pasted in.
And if we hop into our comment, we can set up a broadcast_to for that post. The broadcast_to will broadcast all update made to that comment, whether a new comment is updated or an existing comment is changed in some way or even one deleted, and send it back out to a channel on action cable named after the post association that this comment belongs to!
And that is basically it. Now if I go over here and I add a comment to one of these, you see the comment was added on the left immediately at the same time. That's all web sockets automatically happening through action cable. And we can do it , of course the other way as well.
Kamal:
All right, that is very neat. Now, let's go to production! Because of course, you're not just here to create a Hello World app that runs on your own machine, you want to get this out into the world! And rails8 and forward ships with Kamal, a simple tool for deploying your web application anywhere.
Docker Hub:
And there's a default configuration file in config/deploy.yml that we can use, it's prefilled a little bit, it has the service name of the name of reaction, but we need to rename, for example, your user,the name of the image to go to my name of where I store this on Docker Hub. You can see we change that down in the registry as well and the name of the container image.
Now I'm gonna deploy this on my own little hoppy server. And that hoppy server is currently wiped. it is completely clean, and Ubuntu 24.04 setup that has nothing on it already, this is part of the magic of Kamal, you can spin up a new VM anywhere in the cloud or use your own hardware and point Kamal straight to it, and you'll be going in no time!
21:28:
So this server exists on this address, servers web: - demo.exitsoftware.io ; proxy: host: alpha.exitsoftware.io; And I will then fill out the host as a C name to that machine. But we're using Alpha here, if I had deployed another application called Bravo to the same server, Kamal would set it up, so it's like I host two applications or any number of applications on that same server! Now, we will also need to have a look at the secrets here, that is in dockyml/secrets, because the register that I'm using, that is Docker Hub, needs of course a password, it is using my username but also needs a password.