LoginSignup
181
180

More than 5 years have passed since last update.

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

Last updated at Posted at 2012-09-22
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

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

181
180
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
181
180