[Ruby on Rails] rails g scaffold가 뭐예요?DB 생성 방법
8245 단어 Rails
[Ruby on Rails] rails g scaffold란
scaffold는'비계'라는 뜻이다.rails에서는 기본 DB에 필요한 것들을 함께 만들어요.
서버를 시작하면 DB 일람을 보여주는 페이지, 데이터를 포함하는 추가·편집·삭제 기능을 갖춘 웹사이트가 만들어진다.
카탈로그
작성된 주요 파일
DB 생성 후 처리
명령하다
rails g scaffold モデル名 カラム名1:データ型1 カラム名2:データ型2,,,,
봉화데이터 형식을 지정하지 않으면string입니다
명령 예
$ rails g scaffold games name players
처리 내용
invoke active_record
create db/migrate/20200825114158_create_games.rb
create app/models/game.rb
invoke test_unit
create test/models/game_test.rb
create test/fixtures/games.yml
invoke resource_route
route resources :games
invoke scaffold_controller
create app/controllers/games_controller.rb
invoke erb
create app/views/games
create app/views/games/index.html.erb
create app/views/games/edit.html.erb
create app/views/games/show.html.erb
create app/views/games/new.html.erb
create app/views/games/_form.html.erb
invoke test_unit
create test/controllers/games_controller_test.rb
invoke helper
create app/helpers/games_helper.rb
invoke test_unit
invoke jbuilder
create app/views/games/index.json.jbuilder
create app/views/games/show.json.jbuilder
create app/views/games/_game.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/games.coffee
invoke scss
create app/assets/stylesheets/games.scss
invoke scss
create app/assets/stylesheets/scaffolds.scss
db 폴더의migrate, 모델,views, controllers,assets 등 주요 폴더에 새 파일을 만들었습니다.작성된 주요 파일
1.controllers 폴더
controllers 폴더에서 모델 이름controller.rb를 만듭니다.

DB 작업에 필요한 7개의 동작, index, 쇼, new, edit,create, 업데이트,destroy, 처리를 기록합니다.
2. 모델 폴더
모델 폴더의 concers 폴더에 모델 이름이 있습니다.rb를 만듭니다.

class Game < ApplicationRecord
end
미작성 처리.필요에 따라validate 등의 처리를 기술합니다.models의 파일 설명 예시
class User < ApplicationRecord
validates :name,
presence: {message: "名前を入力してください。"}
end
3. 보기 폴더
views 폴더에 있는 모델 이름의 폴더에 동작 이름이 있는 템플릿을 생성합니다.

각 템플릿은 이미 기술되어 있다.DB의 일람표시, 편집, 상세 페이지 등이 된다.

4.db 폴더
db 폴더의migrate 폴더에 있는 날짜와 시간create 모델 이름입니다.rb 생성

migrate
class CreateGames < ActiveRecord::Migration[5.0]
def change
create_table :games do |t|
t.string :name
t.string :players
t.timestamps
end
end
end
DB와 레일스를 연계한 디자인북을 만들었다.5.assets의javascript 폴더
모델 이름.coffee 파일이 생성되었습니다.

6.assets의 css 폴더
모델 이름.scss와 scaffolds.scss 파일 두 개를 만들었습니다.

7. local 폴더
local 폴더의routes.rb파일에 추가
resources :games
.Rails.application.routes.draw do
resources :games
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
▶ 처리 내용 보기실행
$ rake routes
을 통해 처리된 내용을 확인할 수 있습니다.user1:~/environment/boardgame-app $ rake routes
Prefix Verb URI Pattern Controller#Action
games GET /games(.:format) games#index
POST /games(.:format) games#create
new_game GET /games/new(.:format) games#new
edit_game GET /games/:id/edit(.:format) games#edit
game GET /games/:id(.:format) games#show
PATCH /games/:id(.:format) games#update
PUT /games/:id(.:format) games#update
DELETE /games/:id(.:format) games#destroy
DB 생성 후 처리
migration 실행
scaffold에서 자동으로 생성된 이전 파일을 이용하여 DB에서 표를 작성합니다.
$ rake db:migrate
서버 시작
db에서 표를 완성한 후 서버를 시작합니다.
$ rails s
참고 자료
> Rails 공식 migrate 정보
Reference
이 문제에 관하여([Ruby on Rails] rails g scaffold가 뭐예요?DB 생성 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/shizen-shin/items/627a8857ab46d922163d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)