Add & save from rails database creation


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 ="Hello World")
//Create a "post instance" with content of Hello World


> 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"
    # ]

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 %>
    <% 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


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

Submit from the post form

form_tag method


redirect_to method

  def create
        <div class="form">
        <div class="form-body">
            <textarea name="content"></textarea>
            <input type="submit" value="Post">

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 = params[:content])

Edit and delete posts

Editing function

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

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