ruby 1.9.3과 rails 3.2.2버전을 사용했다.
대부분의 샘플이 DB와 연계되어 만들어지는 튜토리얼이 대부분이라
값이 어떻게 VIEW로 전달되는지 알기가 힘들어서 삽질하다가 정리한다.


[SETP1]. rails로 웹페이지를 띄워보자
rails가 설치되었다고 가정하면, mytest라는 공간에 생성해보자.
오류없이 실행했다면, 아래와 같이 실행 한다.

$ rails new mytest
...
$ cd mytest
$ rails mytest

성공적이라면 http://127.0.0.1:3000 에 접속하면 아래와 같은 화면을 볼수 있다.
 






[SETP2]. 컨트롤러를 통해 Hello world를 출력해보자.

1)  public/index.html 지워준다. 그러면 아래와 같이 오류메시지가 출력된다.


2) 컨트롤러 파일을 생성해보자. 여기서는 home 컨트롤러를 생성한다.

D:\Code\mytest>rails g controller home
      create  app/controllers/home_controller.rb
      invoke  erb
      create    app/views/home
      invoke  test_unit
      create    test/functional/home_controller_test.rb
      invoke  helper
      create    app/helpers/home_helper.rb
      invoke    test_unit
      create      test/unit/helpers/home_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/home.js.coffee
      invoke    scss
      create      app/assets/stylesheets/home.css.scss


3. 생성된 파일 (home_controller.rb)에 다음과 같은 코드를 넣어본다.


4. routes.rb 에 페이지가 뜨도록 경로를 잡아준다.
참고로 우리가 만든 컨트롤러명이 home이고
문자를 출력하는 함수명은 index이다.

그래서 'home#index'라는 이름으로 설정하는것이니 참고...


5. 페이지를 열어보면 짜잔!!! Hello World출력








[SETP3]. erb파일을 이용한 로직과 뷰영역 분리해 Hello World출력

1. 아까 작성했던 내용에서 아래와 같이 수정한다.
    @test라는 멤버변수에 "hello world"라는 값을 넣고, 전달하는것이다.
    여기서는 문자열이지만, map이나 배열도 전달 가능하다.

home_controller.rb와 app/views/home/index.html.erb 가 서로 연계될 수 있는건,
파일명과 위치의 규칙을 맞췄기 때문에 특별한 세팅없이 출력이 가능하다.
app/views/home/index.html.erb
-> home : 생성한 컨트롤이름
-> index : 구현한 함수 이름

 

2. 그러면 이렇게 짜잔...출력


+ Recent posts