일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- Ruby On Rails
- dockerfile
- MySQL
- javascript
- react
- laravel
- controller
- ruby
- Git
- Python
- php
- working directory
- pagination
- vue
- cron
- Wordpress
- ubuntu
- docker-compose
- staging area
- codeigniter4
- NGINX
- imagemagick
- ec2
- Selenium
- SQL
- props
- centOS7
- docker
- AWS
- linux
- Today
- Total
내가 한 노력들
[ Ruby on Rails ] 시작하기 본문
새로운 프로젝트 생성
rails new <프로젝트 명>
정상적으로 생성이되면, 위와같은 파일들이 생성이 된다.
중요한 App폴더와 config폴더에 대한 설명을하자면,
App폴더
Ruby on Rails는 MVC디자인을 사용하는데, App폴더안에 Model, View, Controller 폴더가 들어있는 것을 확인할 수 있습니다.
config폴더
Ruby on Rails를 사용하는데, 또 중요한 Routes파일이 config에 위치해있습니다.
서버실행
rails server
로컬서버인 localhost:3000으로 가보면, 위의 사진과같은 메인 Ruby on Rails 화면이 나오면 성공입니다.
MVC구조로 라우트 하는법
config/routes.rb
Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root :to => "blog#index"
end
root : to => "blog#index" 부분은
root는 localhost:3000의 URL을 의미하고, to를 통해서 blog라는 controller의 index 함수를 연결한 것입니다.
App/controllers/blog_controller.rb
class BlogController < ApplicationController
def index
end
end
def index
end
로 index 함수를 선언을 해줍니다.
App/views/blog/index.erb
<center>
<h1>안녕하세요</h1>
</center>
이젠, 다시 홈페이지를 새로고침을 해보면
안녕하세요가 화면에 보이는 것을 볼 수 있습니다.
여기서 주의할점은
controller와 view를 연결해줄 때, controller의 이름과 view의 폴더명이 같아야합니다 (blog)
그리고 controller의 함수명과 .erb 파일명과 이름이 같아야합니다. (index)
그러면 매인 URL이 아닌, 다른 URL을 route해주고 싶은 경우
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root :to => "blog#index"
get "/home" => "blog#index"
end
get "/home" => "blog#index"
/home URL일 경우에도 blog controller의 index 함수와 연결되어있다.
그럼, http://127.0.0.1:3000/home을 입력해봐도 똑같이 "안녕하세요"화면이 나오는 것을 볼 수 있습니다.
'IT 공부 > Ruby On Rails' 카테고리의 다른 글
[ Ruby on Rails ] 유용한 라이브러리 사용방법 (0) | 2021.09.18 |
---|---|
[ Ruby on Rails ] Pagination 기능 (0) | 2021.09.15 |
[ Ruby on Rails ] Prefix 와 Path (0) | 2021.09.14 |
[ Ruby On Rails ] CRUD 구현하기 (Create) (0) | 2021.09.08 |
[ Ruby on Rails ] Windows 환경에서 Ruby on Rails 설치하기 (0) | 2021.09.06 |