Help us understand the problem. What is going on with this article?

Railsでアプリ作るときに、何回もおんなじこと調べてる気がするので、まとめる。

More than 5 years have passed since last update.
version
ruby: '1.9.2'
rails: '3.2.3'

heroku

$ heroku create app-name --stack cedar
Gemfile
group :production do
  gem 'pg'
  gem 'thin'
end

でgem追加。

bundle
Procfile
web: bundle exec thin start -p $PORT

をアプリのrootフォルダに追加。

config/environments/production.rb
# config.assets.compile = false を
config.assets.compile = true

Bootstrapを使う

Gemfile
gem 'twitter-bootstrap-rails'
$ bundle
$ rails g bootstrap:install
$ rails g bootstrap:layout application fixed # fixed layoutの場合

設定を反映するためにサーバの再起動をする

パスワードにbcrypt-rubyを使って、サインインしたユーザだけが見れるようにする

まずはUser modelを作る

$ rails g model User account:string password_digest:string
$ rake db:migrate

password_digestの方は変更したらだめ。

models/user.rb
has_secure_password

attr_accessible :password, :password_confirmation

を追加。

フォーム作ってもいいけど、ちょっと話ずれるので、consoleで話済ませます。

$ rails c
> User.create!(:account => "shimojik", :password => "kenta", :password_confirmation => "kenta")

セッションコントローラの作成

$ rails g controller sessions

ルーティング

config/routes.rb
resources :sessions

コントローラ

controller/session_controller.rb
def index
  render "new"
end

def create
  user = User.find_by_account(params[:account])
  if user && user.authenticate(params[:password])
    session[:user_id] = user.id
    redirect_to root_path
  else
    flash.now.alert = "Invalid"
    render "new"
  end
end

def destroy
  session[:user_id] = nil
  redirect_to root_path
end

サインイン用フォーム

views/sessions/new.html.erb
<%= form_tag sessions_path do %>
  <div class="field">
    <%= label_tag :account, "アカウント" %>
    <%= text_field_tag :account, params[:account] %>
  </div>
  <div class="field">
    <%= label_tag :password, "パスワード" %>
    <%= password_field_tag :password, params[:password] %>
  </div>
  <div class="actions">
    <%= submit_tag "ログイン" %>
  </div>
<% end %>

current_userメソッドの追加

controllers/application_controller.rb
helper_method :current_user

private
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

ルーティング追加

config/routes.rb
match "/signin" => "sessions#new", :as => :signin
match "/signout" => "sessions#destroy", :as => :signout
def authorize
  if session[:user_id]
    @current_user = User.find(session[:user_id])
    session.delete(:user_id) unless @current_user
  end
end

名前空間を使う(管理者ページなど)

config/routes.rb
namespace :admin do
  # ルーティング
end
$ mkdir app/controllers/admin

adminにおけるスーパークラスを作る

controllers/admin/base.rb
class Admin::Base < ApplicationController
  # before_filter などはここに入れるといい
end

コントローラgenerate

$ rails g controller admin/home --skip-assets

ApplicationControllerじゃなくて、Admin::Baseをスーパークラスに設定

controllers/admin/home_controllers.rb
class Admin::HomeController < Admin::Base
  def index
  end
end

viewsはapp/views/adminフォルダの中に書いていけばいい。
(今回の場合はapp/views/admin/home/index.html.erbなど)

adminのページかどうかはコントローラで判断

controller.kind_of?(Admin::Base)

特定のコントローラやアクションに特定のレイアウトを適応

samples_controller.rb
layout 'admin' # このcontroller全体

def index
  render(:layout => "check") # このactionのみ
end

herokuでmail送信

$ heroku addons:add sendgrid:starter
config/initializers/mail.rb
ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],
  :password       => ENV['SENDGRID_PASSWORD'],
  :domain         => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp

Mailer

Mailerをgenerate

$ rails g mailer NoticeMailer sendmail
mailers/send_mailer.rb
default :from => 'shimojik@example.com'
def sendmail(user)
  @user = user
  mail to: :user.email, subject: "タイトル"
end
app/views/notice_mailer/sendmail.text.erb
# erb形式で内容を書く。
# mailers/sendmail.rbに書いたインスタンス変数は使える

あとは、アクションに追加。

controllers/some_controller.rb
def add_user
  user = User.find(params[:id])
  @mail = NoticeMailer.sendmail(user).deliver
  render text: "メールを送信しました"
end

他にもいろいろ在るので、追加していきます。みなさんのも教えて下さい:)

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away