[Rails] How to display information stored in the database in view

Content of the post

This time I will post about how to display the information stored in the database in the view. In addition, I will post as if the information is already saved in the database.

The implementation flow is as follows.

① Description of controller action (show action) ② When the information you want to display is a character string ③ When the information you want to display is multiple images

environment

Rails 5.2.4.3 ruby 2.5.1 mysql 14.14 view is implemented in haml

① Description of controller action (show action)

This time we want to display the information on the item details page, so we use the ʻitems controller show action`.

items.controller.rb


class ItemsController < ApplicationController
#abridgement

  def show
    @item = Item.find(params[:id])
  end

#abridgement

Explanation) First, search the id of the path of the ʻItem model with the find method, and create an instance of the product (item) that belongs to the corresponding id. `← I'm sorry if it's hard to understand ...

Based on the instance created here, extract information from the database.

② When the information you want to display is text

image.png If the information stored in the database is like the image above, you can display it in the view by specifying = @ item. Column name.

For example, if you want to display the information in the name column, you can do as follows.

show.html.haml


%h2.item-show-page__item-name
  = @item.name

③ When the information you want to display is multiple images

This time, I want to display multiple images stored in the item_images table, which is associated with the items table.

show.html.haml


- @item.item_images.each do |image|
  = image_tag image.image.url

Explanation) First, convert the item_images table that is associated with the item s table (@item) to the block variable image. Next, using ʻimage_tag, call the image of the block variable image (item_images table) converted earlier and add .url`.

This .url is required when selecting information from the database and displaying it in the view. If there is no .url, the image URL stored in the database will be displayed in view.

The rest is iterative processing with the ʻeach statement`.

Finally

This time I posted about how to display the information stored in the database in the view. It is no exaggeration to say that this is the method that is always used when developing applications with Rails, so please refer to it.

Thank you for reading through to the end!

Recommended Posts

[Rails] How to display information stored in the database in view
[Rails] How to display an image in the view
[rails] How to display parent information in child view in nested relationships
How to set the display time to Japan time in Rails
[rails] How to display db information
[Rails] How to reset the database in production environment (Capistrano version)
How to write the view when Vue is introduced in Rails?
How to check Rails commands in the terminal
How to display the text entered in text_area in Rails with line breaks
[Rails] How to load JavaScript in a specific view
[Rails] How to write in Japanese
How to introduce jQuery in Rails 6
[Rails] How to display the list of posts by category
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
How to install Swiper in Rails
[Rails / Routing] How to refer to the controller in the directory you created
[Rails] How to omit the display of the character string of the link_to method
How to store the information entered in textarea in a variable in the method
[Rails 5] How to display the password change screen when using devise
How to implement search functionality in Rails
How to change app name in rails
How to use custom helpers in rails
How to insert a video in Rails
[Rails] How to use the map method
[Rails] How to display error messages individually
[rails] How to configure routing in resources
How to get the date in java
How to implement ranking functionality in Rails
[Rails] How to use video_tag to display videos
[Rails] Reset the database in the production environment
How to use credentials.yml.enc introduced in Rails 5.2
How to display error messages in Japanese
Gorigori beginners summarized how to operate the database using rails console Ntiunus
[Rails] Use devise to get information about the currently logged in user
How to get the setting value (property value) from the database in Spring Framework
I want to display an error message when registering in the database
How to debug the processing in the Ruby on Rails model only on the console
[Rails] How to apply the CSS used in the main app with Administrate
[Rails] How to decide the destination by "rails routes"
How to check the logs in the Docker container
[Ruby on Rails] How to display error messages
I tried to organize the session in Rails
How to display a web page in Java
[Rails] How to use select boxes in Ransack
How to prevent direct URL typing in Rails
How to add sound in the app (swift)
How to delete the database when recreating the application
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
[Japanese localization] i18n rails Easy Japanese localization view display only
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
How to display the result of form input
[Rails] Added in devise: username not added to database
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
How to build the simplest blockchain in Ruby
[Rails] How to use PostgreSQL in Vagrant environment
[Rails] How to convert UC time display to Japanese time display
[Rails] How to register multiple records in the intermediate table with many-to-many association