From the course: Hands-On Introduction: Ruby on Rails

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Adding the image field

Adding the image field

- [Instructor] With active storage already configured, let's add an image field to the post, open the model, and after the validators, add has_one_attached image. With this, Rails takes care of the functionality in the backend. Since the image will be the main element in this application, let's make it mandatory in the validator. Now we're going to add the field to the form. Open the form partial, and let's place it before the description div. Create a new div with the same class, inside it add the label, and then the file field. Let's add some styles. It will be a block with some vertical padding and a little top margin. Let's see how it looks. I did the first one and here's the file field, but as I explained in a previous video, Rails has a security feature called strong parameters, which permits only the specified fields in the controller. You can find it in the post_params method. Just add the new field to the permit method. Now the feature is implemented. Let's test it. Select an…

Contents