Git Product home page Git Product logo

rails-crud's Introduction

Rails CRUD

1. ORM (Object Relational Mapper)

2. Controller 생성

$ rails g controller post index new create show edit update destroy

3. Model 생성

$ rails g model post
  • app/model/post.rb
  • db/migrate/20180619_create_posts.rb
  • migration 파일 변경
# db/migrate/migrate/20180619_create_posts.rb
class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.text :body
      t.timestamps null: false
    end
  end
end
$rake db:migrate
#진짜 table을 만든다
== 20180619043119 CreatePosts: migrating ======================================
-- create_table(:posts)
   -> 0.0089s
== 20180619043119 CreatePosts: migrated (0.0098s) =============================
  • db/schema.rb에 반영이 되었는지 확인

CRUD

  • Create : new, create

  • Read : show

  • Update : edit, update

  • Destroy : destroy

  • Create

irb(main):001:0 > Post.create(title: "제목", body: "내용")
   (0.1ms)  begin transaction
  SQL (5.3ms)  INSERT INTO "posts" ("title", "body", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["title", "제목"], ["body", "내용"], ["created_at", "2018-06-19 08:29:59.313013"], ["updated_at", "2018-06-19 08:29:59.313013"]]
   (3.9ms)  commit transaction
=> #<Post id: 1, title: "제목", body: "내용", created_at: "2018-06-19 08:29:59", updated_at: "2018-06-19 08:29:59">
  • Read
irb(main):001:0 > Post.find(id)
   (0.1ms)  begin transaction
  SQL (5.3ms)  INSERT INTO "posts" ("title", "body", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["title", "제목"], ["body", "내용"], ["created_at", "2018-06-19 08:29:59.313013"], ["updated_at", "2018-06-19 08:29:59.313013"]]
   (3.9ms)  commit transaction
=> #<Post id: 1, title: "제목", body: "내용", created_at: "2018-06-19 08:29:59", updated_at: "2018-06-19 08:29:59">
irb(main):002:0> Post.find(1)
  Post Load (1.9ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", 1]]
=> #<Post id: 1, title: "제목", body: "내용", created_at: "2018-06-19 08:29:59", updated_at: "2018-06-19 08:29:59">

  • update
irb(main):001:0 > post = Post.find(id)
irb(main):002:0 > post.update(title: "변경", body: "변경")
irb(main):003:0> Post.find(1).update(title: "변경", body: " 변경")
  Post Load (1.8ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
  SQL (8.1ms)  UPDATE "posts" SET "title" = ?, "body" = ?, "updated_at" = ? WHERE "posts"."id" = ?  [["title", "변경"], ["body", "변경"], ["updated_at", "2018-06-19 08:31:14.239322"], ["id", 1]]
   (5.7ms)  commit transaction
=> true
  • Destroy
irb(main):001:0 > Post.find(id).destroy
  Post Load (1.9ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
  SQL (6.5ms)  DELETE FROM "posts" WHERE "posts"."id" = ?  [["id", 1]]
   (5.6ms)  commit transaction
=> #<Post id: 1, title: "변경", body: "변경", created_at: "2018-06-19 08:29:59", updated_at: "2018-06-19 08:31:14">
def destroy
  flash[:alert] = "삭제되었습니다."
end
<%= flash[:alert]%>

app/views/layout/_flash.html.erb

<%= render 'layout/flash'%>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.