디바이즈 젬으로 인증하기
Created by Piotr Steininger, @polishprince
Updated by Ernesto Jimenez, @ernesto_jimenez
앱 개발 가이드를 따라서 레일스 걸스 앱을 미리 만들어 두어야 합니다.
1.Add devise gem
Gemfile 파일을 수정합니다. 아래와 같이 젬을 추가합니다.
gem 'devise'그리고 아래와 같이 실행합니다.
bundle install젬을 설치한 다음 레일스 서버를 꼭 다시 실행합니다.
2.Set up devise in your app
아래와 같이 터미널에서 명령어를 실행합니다.
rails g devise:install3.Configure Devise
환경 설정 파일에서 기본 url 옵션을 정의해야 합니다. config/environments/development.rb 을 열어 아래와 같이 :
   config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }end 키워드 위에 추가합니다.
app/views/layouts/application.html.erb 파일을 아래와 같이 수정합니다:
<% if notice %>
  <p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
  <p class="alert alert-danger"><%= alert %></p>
<% end %>위 코드를 아래 코드 위에 넣습니다.
   <%= yield %>app/views/ideas/show.html.erb 파일에서 아래와 같은 부분을 지우고 위에서 넣은 코드를 넣습니다:
<p id="notice"><%= notice %></p>app/views/comments/show.html.erb 파일도 마찬가지로 수정합니다. 파일마다 코드를 수정하지 않아도 app/views/layouts/application.html.erb 파일만 수정해도 됩니다.
4.Setup the User model
디바이즈 젬이 만들어둔 스크립트를 사용해서 사용자(User) 모델을 만들겠습니다.
   rails g devise user
   rake db:migrateCoach: 사용자(user) 모델이 생성한 것을 설명하세요. 필드는 무엇인가요?
5.Create your first user
사용자를 등록할 수 있는 모든 준비가 되었습니다. 디바이즈는 계정을 생성하고 로그인, 로그아웃하는 모든 코드와 라우드를 만들어줍니다.
레일스 서버를 실행하고, 브라우저로 http://localhost:3000/users/sign_up로 이동하면 사용자 계정을 생성할 수 있습니다.
6.Add sign-up and login links
다음으로 사용자가 로그인하는 링크와 알림 메시지를 네비게이션 바의 오른쪽 위에 넣겠습니다.
우선 app/views/layouts/application.html.erb 파일을 아래와 같이 수정합니다:
<p class="navbar-text pull-right">
<% if user_signed_in? %>
  Logged in as <strong><%= current_user.email %></strong>.
  <%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> |
  <%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link'  %>
<% else %>
  <%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link'  %> |
  <%= link_to "Login", new_user_session_path, :class => 'navbar-link'  %>
<% end %>
</p>위의 코드를 아래 코드 아래에 넣습니다.
<ul class="nav">
  <li class="active"><a href="/ideas">Ideas</a></li>
</ul>그런 다음 로그인하지 않았을 경우 사용자가 로그인 페이지로 이동하도록 app/controllers/application_controller.rb 파일을 아래와 같이 수정합니다:
  before_action :authenticate_user!위의 코드를 protect_from_forgery with: :exception 뒤에 붙여 넣습니다.
브라우저를 열어서 로그인과 로그아웃을 해봅니다.
Coach: user_signed_in?과 current_user 헬퍼를 설명하세요. 헬퍼는 왜 유용한가요?
What next?
- 사용자(User) 모델에 필드를 더 추가합니다.
 - 사용자와 아이디어의 관계를 추가합니다.
 - 다른 사용자가 등록한 아이디어를 수정하거나 삭제하지 않도록 제한합니다.
 - 권한과 역할(캔캔 CanCan처럼 유명한 인증 젬을 사용하여) 기능을 확장합니다.