Rails 6 Blog - CURD

Опубликовано: 05 Октябрь 2024
на канале: MAkeEAsy
47
1

rails generate model Post title:string content:text

rails db:migrate

rails console # or rails c

CREATE:

post = Post.create(title: "Title of Post", content: "Content of Post")

READ:

Post.all

post = Post.find_by(title: 'Title of Post')
post = Post.where(title: 'Title of Post').first
posts = Post.all.order(created_at: :desc)
posts = Post.all.order(created_at: :asc)

UPDATE:

post = Post.find_by(title: 'Title of Post')
post.title = 'New Title of Post'
post.save

DELETE:

post = Post.find_by(title: 'Title of Post')
post.destroy

CURD with scaffold

rails generate scaffold_controller Post title:string content:text


Buy me a coffee - https://www.buymeacoffee.com/t8lUVvp

fake online REST API
https://fakerestapi.com/ - REST API for developers to test and prototyping of sample applications.