[RAILS] index action settings My memo

Note database.yml can describe database settings RDB is a database that can manage data in tabular format RDBMS is a system that manages RDB, and there is MySQL in RDBMS! !!

A library is a collection of complex functions. Gem information is managed in Gemfile

When describing the contents to be recorded in the database in the migration file

def change create_table :tweets do |t| t.string :name t.text :image end

Described as t. type: Described in the form of column name Use the rails db: migrate command to reflect in the database

-When adding data to the column name created by the flow up to this point Launch console

Tweet.create(name: "takashi", text: "Nice to meet you!")

Now you can save (create) information in the name column and image column of the tweets table created from the Tweet model. → But if you don't specify the record number, you can't save it ... ?? I thought, but it seems that the record number will be 1 without permission

Tweet list display

Define an index action for your controller Write to the routes.rb file

Use the resources method as a method to automatically generate routes to 7 actions

Rails.application.routes.draw.do resources :tweets end

→ The symbol: tweets is specified as an argument of resources. Then, the routing corresponding to the path of/tweets is generated!

Display function In other words, if you want to specify only the index action, write resources: tweets, only :: index

Controller edition

Make a controller named tweets with rails d controller tweets! Edit tweets_controller.rb!

Specify the path of app/controllers/tweets_controller.rb

Describe index action in class TweetsController

def index @tweets = Tweet.all end

→ Now you can assign all the records in the tweets table to the instance variable and send it to the view.

View

Create a file called index.html.erb. View all tweet posts here Use the each method when you want to extract all the information in the tweets table!

@tweets.each do |tweet|

tweet.text tweet.name

end

Now you can extract the information from the text and name columns of the tweets table (by the each method) from all the records and display it on the post list screen!

I omitted all <%%> etc. and described only the important points

About layout templates → Describe the parts that are common to all view files, such as headers and footers. Described in a file called application.html.erb It exists in app/views/layouts/application.html.erb

The description of each view file is aggregated in <% = yield%> in the body element of application.html.erb.

Regarding CSS description →  app/assets/stylesheets/application.css Describe in

All the descriptions of ●● .css existing in the file called stylesheets are read into application.css!

Recommended Posts

index action settings My memo
[My style] rails Initial setting memo