Add & save from rails database creation

Introduction

Create a web app that can do CRUD (create, read, update, delete) like the most basic Twitter app.

Creating a table and reflecting its contents

$ rails g model Post content:text
//rails g model table name(Singular)Column name:Data type(What kind of data will be entered)

$ rails db:migrate
//Reflect the contents of the migration file = table(DB)Can

The table name is singular.

$ rails console

> post = Post.new(content:"Hello World")
//Create a "post instance" with content of Hello World

> post.save  

> posts = Post.all
> posts[0] //posts[0]Can be confirmed

In the action index in the posts controller, Create a function index that receives the contents of the blog from the table.

class PostsController < ApplicationController
  def index
    @posts = Post.all
    # @posts = [
    #   "I'm studying Rails from today!",
    #   "Post list page is being created!",
    #   "I ate a snack today!",
    #   "I want to eat chocolate",
    #   "It was sunny after a long time today"
    # ]
  end
end

If you add @ to the variable, you can access it from other files, so use @posts. It can be displayed in html like this.

  <div class="container">
    <% @posts.each do |post| %>
        <div class="posts-index-item">
            <%=post.content %>
        </div>
    <% end %>

When css is arranged, it looks like this. I was able to add it from the rails console! !! !! !! スクリーンショット 2020-10-20 20.24.50.png

Put together html

Write the HTML you want to display on any page in application.html.erb Like the header.

A method that makes it easy to create links

Write this in html with <li> </ li>.

<%=link_to("TweetApp","/") %>

Let's make a new posting screen

Check posts with id: 3

$ rails console

> post = Post.find_by(id: 3)
> post.content
> post.created_at #Posting time

Route

  #Post detail page routing
  get "posts/:id" => "posts#show"

Submit from the post form

form_tag method

<%=form_tag(url)%>
<%end%>

redirect_to method

  def create
    redirect_to("/posts/index")
  end
    <%=form_tag("/posts/create")%>
        <div class="form">
        <div class="form-body">
            <textarea name="content"></textarea>
            <input type="submit" value="Post">
        </div>
        </div>
    <%end%>

Since it is saved as content: (posted content) in a hash table called params, New, save and redirect as below

  #Add create action
  def create
    @post = Post.new(content: params[:content]) 
    @post.save
    redirect_to("/posts/index")
  end

Edit and delete posts

Editing function

rails console
> post = Post.find_by(id: 1)
> post.content = "apple" #Overwrite
> post.save

Delete function

rails console
> post = Post.find_by(id: 1)
> post.destroy

Recommended Posts

Add & save from rails database creation
Add, save, delete, add columns, save images from rails DB creation
Add binding.pry (rails)
Rails database basics
[Rails] Many-to-many creation
Save data from excel file using Rails gem roo
[Rails] Save information from one form to multiple tables
[Ruby on Rails] From MySQL construction to database change
Cloud9 (Rails) from Github
Portfolio creation Ruby on Rails
[Rails] Save images using carrierwave
API creation with Rails + GraphQL
Rails reference type creation added
[Rails] Add / Remove Forms (cocoon)
[Rails] Add column to devise
[Rails] New app creation --Notes--
Aggregate Rails enums from SQL