Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Lessons:14Length:2.4 hours
  • Overview
  • Transcript

2.5 Adding Comments: Part 2

This is part two on implementing a comment system into our tasks. Let's continue on with developing the form we left behind in the previous lesson. Inside this form method, I'm gonna have some sort of a paragraph that will have the name. The name will be enclosed in a text field. So we use the text field option. Then, we specify the name which will create an input that will be easily parsable by the Rails framework and thus making it very easy. Next we're going to provide the email. So lets create another paragraph, and then another text view. Actually we can type in email field. Because that's a method available in the Rails framework. It's basically the very same but it's better for mobile devices.. If I reload the page you will see that. Let me just take the chance and provide a placeholder. Use the placeholder method. And this will actually create a placeholder text on top of that field. So we'll do that in here as well so email it is. Reel in the page and there you go. So I can hover over it and click on it and change it. Okay, pretty good. Now let's go ahead and move on to the next step which is providing a text area for the actual body. Now if I type in F.text_area for that body. There's your work. Text area is another method available in the form builder class. Check the link in the description below to know about all of the input generators. Okay, now reloading the page, you will see that new text area. If I want to I can increase the number of rows up to eight, and the columns as well for example, 50. These are generic HTML attributes. You can put them in there and Rails will interpret it just fine. Reloading the page, and there you go, a much larger box. To end this all, I just need a submit tag. What better way to do it than using the f.submit tag. If I don't specify any text that goes into the submit tag. Rails will try and create a text for you. In this case it tells us create comment. Well that's pretty appropriate, we are indeed creating a comment. If we were trying to create a task, it would be called create task. So that's pretty smart as a framework. Okay, so I guess we can now create our very own comment. If I type in my name here, and then my address, for example this one. And then type in my first comment. If I try and press create comment this will thrown an error. Because we don't have the comments controller. That's easy to fix. If Adobe can use the Rails generate controller method, I'm not going to do that. It will generate a bunch of different files that really don't fit what we need. It would create a bunch of different views and templates, depending on the actions that we would specify the controller, but in this case we really don't want any views. Let's create the comments controller really quick inside the controllers method. Let's open it up and define the comments controller. The comments controller will inherit from the application controller you see here. This file right here. Okay with that, I'm gonna go back to the controller, type in the end keyword and define the create action. It's just a ruby method. From here we'll be able to retrieve the comment's data, create a new comment based on the task, and redirect to the page that shows the task. Along with all the other comments. First we need to retrieve the task. And how do we do that? Well there's a method available in active record. So for each different class, in this case it is the task. We type in the find method. The find method takes a string or a number so we use perams. And then a specific key. The parems hash is the data that comes from the form, the request. In this particular case, we want to use a task_id. The task_id is the number that comes in the URL. It is interpreted as this parameter so it gets shoved into the hash. With this we'll be able to retrieve the task, and then we want to comment. Comment is going to be something like task.comments.build with some parameters. The parameters is going to be something like this, params[:comments]. So basically we want to introduce a new comment into the task. This is a solid way of assigning a comment to a task, establishing that relationship. There's one thing that I forgot to mention. And that is we shouldn't really be injecting this data all together. But for now, we're going to stick with this. Now, if the comment is successfully saved, which we do want to, we're going to redirect to the task page. So just pass in the task model, and it should be good to go. We also want a notice message saying something like comment posted. Now, if we don't save the comment. We actually want to redirect to the very same place, but instead of a notice message. We type in an alert message saying comment not posted. It's up to you if you want to change this information, but for now I think this is enough. So let's review this one more time. We're retrieving the task that's passed as a parameter In the URL address, then we're gonna go to the comments. Retrieve the comments and build a new one related to this task with the information that comes from the form. Oh, actually, it should be params[:comment] not comments. Next, we establish the condition. If the comment is saved. We want to tell the user that the comment has been posted. Otherwise, alert the user of the occurrence. Let's see now, let's reload this page and before I do that. Notice this one right here, do you see that? That's the task_id. Okay, let's reload the page and resend all the data. There you go. It's actually a pretty good thing that I've mentioned this and the error popped up. We shouldn't be providing these parameters as they are. We should be using the Strong Parameters Gem. Basically it should receive a filtered list of attributes from the params hash. So we use for example, common_ params. We're going to replace the contents. And switch them with common_params, there you go. And then we'd want to go here, and in stead of resorting to the comment hash right away. We want to use the require method. There you go, basically it's telling us that we should have a comment key in the params hash. To establish some sort of integrity. And then we want to permit a list of variables. In this case the name, the email and the body. What this is telling us is we are supposed to have a hash. That hash is supposed to have a comment. Which in turn is a hash with a name. Which will have some value. The email and also the body. Something like this. We want to establish this rule. So this will not work. I'm gonna go here. Send the whole data again and there you go. Voila. There's our Comment. Congratulations on implementing Comments. Jump into the next lesson to learn a little bit more about the asset pipeline, how we handle CSS, and JavaScript.

Back to the top