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.

Displaying the image

Displaying the image

- [Instructor] Now that you can upload images, you need to display them. To do this, open the post partial. Create some space before the description, and display the image with a image_tag. Then, use the url_for helper, pass in the post.image. This will return the final URL to the image_tag. Let's test it. Start the server. And in the browser, reload the list. We get an error right in the url_for helper. The error is undefined method persisted? for nil. This is because we're trying to display the image of the other posts, which they don't have yet. To fix this, add a condition so that it displays the image only if it exists. This is achieved with the attached? method of the image. Let's take a look. Now, it only shows the first one, but it's displaying it at full size. Let's apply a first solution with CSS. Open the index template. Let's add a tailwind CSS class to see a maximum width to the container. In the parent div, add max-w- and within brackets, 630px. We load the page. Now it…

Contents