[rails] 스캐폴딩 기능 개발 기초 : 목록 화면 작성

2016. 7. 16. 17:17프로그래밍/Ruby on Rails

728x90
728x90
BooksController

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class BooksController < ApplicationController
  before_action :set_book, only: [:show, :edit, :update, :destroy]
  # GET /books
  # GET /books.json
  def index
    @books = Book.all
  end
  # GET /books/1
  # GET /books/1.json
  def show
  end
  # GET /books/new
  def new
    @book = Book.new
  end
  # GET /books/1/edit
  def edit
  end
  # POST /books
  # POST /books.json
  def create
    @book = Book.new(book_params)
    respond_to do |format|
      if @book.save
        format.html { redirect_to @book, notice: 'Book was successfully created.' }
        format.json { render :show, status: :created, location: @book }
      else
        format.html { render :new }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end
  # PATCH/PUT /books/1
  # PATCH/PUT /books/1.json
  def update
    respond_to do |format|
      if @book.update(book_params)
        format.html { redirect_to @book, notice: 'Book was successfully updated.' }
        format.json { render :show, status: :ok, location: @book }
      else
        format.html { render :edit }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end
 # DELETE /books/1
 # DELETE /books/1.json
 def destroy
   @book.destroy
   respond_to do |format|
     format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
     format.json { head :no_content }
   end
 end

 private
   # Use callbacks to share common setup or constraints between actions.
   def set_book
     @book = Book.find(params[:id])
   end

     # Never trust parameters from the scary internet, only allow the white list through.
   def book_params
     params.require(:book).permit(:isbn, :title, :price, :publish, :published, :cd)
   end
end
cs


만약 index 메서드를 다룬다고 쳤을 때

액션에 대응하는 템플릿 파일이 /app/views/books/index.html.erb 파일이 있다.


index.json.builder라는 파일도 있다.

이것은 데이터를 json 폼으로 출력해주는 템플릿이다.

URL/books.json 으로 접속해보면 다음과 같이 나온다.


index.html.erb

1
2
3
    <td><%= link_to 'Show', book %></td>
    <td><%= link_to 'Edit', edit_book_path(book) %></td>
    <td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %></td>
cs


잡다구리한거 빼고 @books 속성 출력부분.


뷰 헬퍼는 템플릿 파일을 작성할 때 도움을 주는 메서드를 의미한다.

link_to가 뷰 헬퍼 메서드로 매개 변수로 하이퍼링크를 생성해준다.


1
link_to(body, url, [, html_options])
cs


link_to 메서드로 특정한 경로 표기(객체)


<td><%= link_to 'Show', book %></td>


book은 each 메서드에서 템플릿 변수 @books로 얻은 하나의 객체이다.

매개 변수 url에 모델 객체를 적용하면 id를 사용하여 /books/id로 가져온다.


link_to 메서드로 특정한 경로 표기(뷰 헬퍼)


<td><%= link_to 'Edit', edit_book_path(book) %></td>


edit_book_path와 new_book_path는 routes.rb에서 resources 메서드를 호출할 때 자동으로 사용되는 뷰 헬퍼이다.

(맨날 회사에서 이게 뭔지 모르고 썼는데 드디어 알았다.)


라우터 정의에 따라 자동으로 생성되는 뷰 헬퍼

헬퍼 이름 

경로 

book_path 

/books 

book_path(id) 

/books/:id 

new_book_path 

/books/new 

edit_book_path(id) 

/books/:id/edit 

링크를 클릭할 때 확인 대화 상자 표시


<td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %>



728x90
반응형