<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>763914974 (running man)</title>
    <link>https://ruby-china.org/763914974</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>整合 ElasticSearch 到现有 Rails 项目</title>
      <description>&lt;h3 id="导言"&gt;导言&lt;/h3&gt;
&lt;p&gt;这两天项目要求，把现有的搜索改成 ElasticSearch(后面简称 es)。之前接触 过一些 es，后来就开始捣鼓。记得 railcasts 上面有讲过相关视频，重温了下就  开始弄，没弄多久发现上面用的&lt;a href="https://github.com/karmi/retire" rel="nofollow" target="_blank" title=""&gt;tire&lt;/a&gt;已经 retire 了。为了让更多的朋友们不走冤枉路，所以才有了此文。&lt;/p&gt;
&lt;h3 id="简介"&gt;简介&lt;/h3&gt;
&lt;p&gt;大致说下什么是 es，详细的&lt;a href="https://en.wikipedia.org/wiki/Elasticsearch" rel="nofollow" target="_blank" title=""&gt;Wikipedia&lt;/a&gt;有介绍。es 其实就是一个搜索的引擎，从开源项目 lucene 出来，lucene 是 java 编写的，比较复杂，要使用它必须了解核心一大堆东西。es 就是包装了一层，然后提供 RESTFUL API 调用，从而让全文搜索变得更加简单。&lt;/p&gt;
&lt;h3 id="过程"&gt;过程&lt;/h3&gt;&lt;h5 id="安装"&gt;安装&lt;/h5&gt;
&lt;p&gt;在安装 es 之前，先安装 jdk。
  Mac 环境，运行 &lt;code&gt;brew install elasticsearch&lt;/code&gt;, 然后运行&lt;code&gt;elasticsearch --config=/usr/local/opt/elasticsearch/config/elasticsearch.yml&lt;/code&gt;启动，访问&lt;a href="http://localhost:9200" rel="nofollow" target="_blank" title=""&gt; http://localhost:9200&lt;/a&gt;，访问成功就表示安装完成了。&lt;/p&gt;

&lt;p&gt;其他环境，访问&lt;a href="http://elasticsearch.org/download" rel="nofollow" target="_blank" title=""&gt;官网&lt;/a&gt;相关安装包下载。 &lt;/p&gt;
&lt;h5 id="使用"&gt;使用&lt;/h5&gt;
&lt;p&gt;在 Gemfile 中加入&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem 'elasticsearch-model' 
gem 'elasticsearch-rails' 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;注意：es-model 自带了分页插件，如果你在 gemfile 中有分页，如&lt;code&gt;will_paginate&lt;/code&gt; 或者 &lt;code&gt;kaminari&lt;/code&gt;，要把他们放到 es-model 和 es-rails 的前面。&lt;/p&gt;

&lt;p&gt;在需要添加搜索的 model 添加以下代码：&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;University&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Elasticsearch&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Model&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Elasticsearch&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Callbacks&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;完成引用后，我们可以编写 search 方法了：&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;__elasticsearch__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;这是一个很简单的 search，通过传入的参数直接进行检索。我们可以使用 DSL 来使我们的检索语句更加满足我们的业务需要，以下是我需要检索一个状态为 1，并且从栏目名为 name 的一个检索：&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;__elasticsearch__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;"query"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"filtered"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="s2"&gt;"filter"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"bool"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="s2"&gt;"must"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;     &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"term"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}},&lt;/span&gt;
              &lt;span class="s2"&gt;"must"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="s2"&gt;"query"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
                  &lt;span class="s2"&gt;"match"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;关于 es 的 DSL 更多写法，大家可以访问&lt;a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_most_important_queries_and_filters.html" rel="nofollow" target="_blank" title=""&gt;这里&lt;/a&gt;。里面详细的讲解了 query,filter 等一些常用查询，大家可以根据业务需要自行改装。&lt;/p&gt;

&lt;p&gt;然后我们为 model 创建 index, 主要给 es 使用：&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;mapping&lt;/span&gt; &lt;span class="ss"&gt;dynamic: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;indexes&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;
  &lt;span class="n"&gt;indexes&lt;/span&gt; &lt;span class="ss"&gt;:tag&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;我们继续往下走，model 是可以 serialized 成 json 的，我们使用&lt;code&gt;as_indexed_json&lt;/code&gt;这个方法。我们可以这样写：&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;as_indexed_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt;
  &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:status&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;   
    &lt;span class="ss"&gt;include: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;tags: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;]}}&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;include&lt;/code&gt;的部分是处理 association 的，&lt;code&gt;only&lt;/code&gt;是 model 本身的字段属性。完成了以上调整，我们的 model 搜索基本完成了。如果你现在使用搜索，我估计还是搜索不出数据。我们要把数据导入给 es，使用这个命令&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rake environment elasticsearch:import:model &lt;span class="nv"&gt;CLASS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'your_model_name'&lt;/span&gt; &lt;span class="nv"&gt;FORCE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;y
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;好啦。基本就完成。es 默认自带中文分词，但是有些 posts 反馈说不大好用，可以使用&lt;a href="https://github.com/medcl/elasticsearch-rtf" rel="nofollow" target="_blank" title=""&gt;es-rtf&lt;/a&gt;，集成了中文的分词插件，下载直接可以用。必须要安装 jdk，里面有详尽的使用方法。&lt;/p&gt;
&lt;h3 id="相关资料"&gt;相关资料&lt;/h3&gt;
&lt;p&gt;gem 的&lt;a href="https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model" rel="nofollow" target="_blank" title=""&gt;README&lt;/a&gt;,有耐心慢慢看可以了解很多&lt;/p&gt;

&lt;p&gt;一些快速的 tutorial&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.sitepoint.com/full-text-search-rails-elasticsearch/" rel="nofollow" target="_blank"&gt;http://www.sitepoint.com/full-text-search-rails-elasticsearch/&lt;/a&gt;
&lt;a href="http://aaronvb.com/articles/intro-to-elasticsearch-ruby-on-rails-part-1.html" rel="nofollow" target="_blank"&gt;http://aaronvb.com/articles/intro-to-elasticsearch-ruby-on-rails-part-1.html&lt;/a&gt;
&lt;a href="http://www.spacevatican.org/2012/6/3/fun-with-elasticsearch-s-children-and-nested-documents/" rel="nofollow" target="_blank"&gt;http://www.spacevatican.org/2012/6/3/fun-with-elasticsearch-s-children-and-nested-documents/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;可以了解，railcast 的 es 介绍 &lt;a href="http://railscasts.com/episodes/306-elasticsearch-part-1" rel="nofollow" target="_blank"&gt;http://railscasts.com/episodes/306-elasticsearch-part-1&lt;/a&gt;&lt;/p&gt;</description>
      <author>763914974</author>
      <pubDate>Wed, 30 Sep 2015 16:13:00 +0800</pubDate>
      <link>https://ruby-china.org/topics/27530</link>
      <guid>https://ruby-china.org/topics/27530</guid>
    </item>
    <item>
      <title>Some common gems for we build Ruby on Rails applications</title>
      <description>&lt;p&gt;In this post, I will share some common gems that we use for building RoR applications.&lt;/p&gt;
&lt;h3 id="bcrypt"&gt;bcrypt&lt;/h3&gt;
&lt;p&gt;Sometimes, We build user authentication features without devise gem, how are we to solve for storing passwords? we use bcrypt. It allows you to easily store a secure hash of users' passwrods.&lt;/p&gt;

&lt;p&gt;Follow some steps below&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install
In your Gemfile, add codes below
&lt;code&gt;
gem 'bcrypt'
&lt;/code&gt;
then runs "bundle install" commands.&lt;/li&gt;
&lt;li&gt;Migration your user table
&lt;code&gt;
add_column :table_name, :password_digest, :string
&lt;/code&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;3.Update user model file.
In your user's model file, add code below&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;has_secure_password
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That's it.&lt;/p&gt;
&lt;h3 id="simple_form"&gt;simple_form&lt;/h3&gt;
&lt;p&gt;We always encounter CURD tasks when we build admin dashboard. simple form gem is a really best helper, it can help us to solve a tons of html tags problems. &lt;/p&gt;

&lt;p&gt;Basic Usage&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;＃ definition form url
&amp;lt;%= simple_form_for @post do |f| %&amp;gt;
＃ model collection, dropdown list html tag
&amp;lt;%= f.input :user_id, collection @users %&amp;gt;
＃ textfield html tag
&amp;lt;%= f.input :content %&amp;gt;
＃ submit
&amp;lt;%= f.submit '提交' %&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="annotate"&gt;annotate&lt;/h3&gt;
&lt;p&gt;You always don't know the schemas when you look at the models. Try annotate gem, it helps you to add comment summaraizing to the current schem.
When you installed and run 'bundle exec annotate', you will find it is very clearly to understand models. Like this:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# == Schema Information
# Table name: users
#  id              :integer          not null, primary key
#  nickname        :string(255)
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#  password_digest :string(255)
#  remember_token  :string(255)
#

class User &amp;lt; ActiveRecord::Base
  before_save { self.email = email.downcase }
  before_create :create_remember_token
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="will_paginate"&gt;will_paginate&lt;/h3&gt;
&lt;p&gt;In Ruby on Rails world, To develop paginate feature is really easy, will_paginate gem helps you handling a lot of thins about pagination. &lt;/p&gt;

&lt;p&gt;How to use? There is a post class need to paginate.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;＃ In model
@posts = Post.paginate(page: params[:paeg]
＃ or 
@posts = Post.paginate(page: params[:page], per_page: 10)
＃ In page
&amp;lt;%= will_paginate @posts %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="puma"&gt;puma&lt;/h3&gt;
&lt;p&gt;Unlike other Ruby Webservers, Puma was built for speed and parallelism. Puma is a small library that provides a very fast and concurrent HTTP1.1 server for Ruby web applications. It is designed for running Rack apps only.&lt;/p&gt;

&lt;p&gt;You can install puma gem very easily, add codes below in your gemfile. &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem 'puma'
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then start your server &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails s Puma
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="better_errors"&gt;better_errors&lt;/h3&gt;
&lt;p&gt;Provides a better page for Rails and other Rack apps. Includes sources code inspection, a live REPL and local/instance variable inspection for all stack frames. &lt;/p&gt;

&lt;p&gt;After we finish installing beter_errors gem, when the error pages appear, like this:
&lt;img src="http://asciicasts.com/system/photos/1446/original/E402I02.png" title="" alt=""&gt;
Woo, It looks very nice, it helps us to find the problems exactly. &lt;/p&gt;
&lt;h3 id="rails_panel"&gt;rails_panel&lt;/h3&gt;
&lt;p&gt;This is an extension for Chrome which adds a development log withn the brower. As we visit various pages in our application we should see an entry for each one in the panel. This workds for AJAX requests too.
&lt;img src="http://asciicasts.com/system/photos/1450/original/E402I06.png" title="" alt=""&gt;&lt;/p&gt;
&lt;h3 id="capistrano"&gt;capistrano&lt;/h3&gt;
&lt;p&gt;Capistrano gem is a remote server automation tool. It help us to deploy a rails server more convenient.
First, you need to install the gem in your gemfile, and bundle it.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;group :development do
  gem 'capistrano', '~&amp;gt; 3.3.0'
  gem 'capistrano-rails'
  gem 'capistrano-bundler'
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The second step, To initial it.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cap install
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The third step, the complicated step, to configu the cap file.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# config valid only for current version of Capistrano
lock '3.3.5'

set :application, '[projectname]'
set :repo_url, 'git@github.com:[username]/[projectname].git'

＃ Default branch is :master
＃ ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call

＃ Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/var/www/[projectname]'

＃ Default value for :scm is :git
set :scm, :git
set :rbenv_type, :system
set :rbenv_path, '/home/deploy/.rbenv'
set :rbenv_ruby, '2.2.0'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}

set :default_environment, {
    'PATH' =&amp;gt; "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
}


＃ puma
set :puma_rackup, -&amp;gt; { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"    #accept array for multi-bind
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'staging'))
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"
set :puma_role, :app
set :puma_threads, [0, 16]
set :puma_workers, 0
set :puma_worker_timeout, nil
set :puma_init_active_record, false
set :puma_preload_app, true


＃ Default value for :format is :pretty
＃ set :format, :pretty

＃ Default value for :log_level is :debug
＃ set :log_level, :debug

＃ Default value for :pty is false
＃ set :pty, true

＃ Default value for :linked_files is []
set :linked_files, %w{config/database.yml config/secrets.yml config/env.yml}

＃ Default value for linked_dirs is []
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}

＃ Default value for default_env is {}
＃ set :default_env, { path: "/opt/ruby/bin:$PATH" }

＃ Default value for keep_releases is 5
 set :keep_releases, 5

set :keep_releases, 5

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
    end
  end

  after :restart, :'puma:restart'
  after :publishing, :restart

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
    end
  end

end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;More detail you can visit our teams book.  &lt;a href="https://zhaozijie.gitbooks.io/intro_rails/content/cong_yi_tai_kong_de_fu_wu_qi_shang_bu_shu_rails_ying_yong.html" rel="nofollow" target="_blank"&gt;https://zhaozijie.gitbooks.io/intro_rails/content/cong_yi_tai_kong_de_fu_wu_qi_shang_bu_shu_rails_ying_yong.html&lt;/a&gt;\&lt;/p&gt;
&lt;h3 id="letter_opener"&gt;letter_opener&lt;/h3&gt;
&lt;p&gt;When we develop mail features, we always want to preview our mail template, and we don't want to sent mail for testing. we use letter_opener gem.&lt;/p&gt;

&lt;p&gt;1 Install&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem 'letter_opener' 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;2  Configuration&lt;/p&gt;

&lt;p&gt;To set the codes in config/environments/development.rb&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;config.action_mailer.delivery_method = :letter_opener
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok, That's it. The email will pop up in your brower, they are stored in tm/letter_openrer.&lt;/p&gt;
&lt;h3 id="rack_mini_profiler"&gt;rack_mini_profiler&lt;/h3&gt;
&lt;p&gt;We use rake_mini_profiler gem to monitor the speed of the rails app.&lt;/p&gt;

&lt;p&gt;First, add codes below to gemfile and install&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem 'rack-mini-profiler' &lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Second, there is no second step if you want :). Restart your server, and visit you app in brower. You will find the speed data on the top-left of brower. Like this:&lt;/p&gt;

&lt;p&gt;&lt;img src="https://l.ruby-china.com/photo/2015/73f11d1e28df940f53e635d16a0e8e7d.png" title="" alt=""&gt;&lt;/p&gt;
&lt;h3 id="rack-attack"&gt;rack-attack&lt;/h3&gt;
&lt;p&gt;We advise you to use the rack-attack gem to protects the rails apps. It allows you to create whitelisting, balcklisting, throttling, and tracking. Let's add the gem to your web apps.&lt;/p&gt;

&lt;p&gt;Install, add it to your Gemfile&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem 'rack-attack'
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Set configuration in the application.rb&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# In config/application.rb
config.middleware.use Rack::Attack 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Add custom codes to rack-attack.rb&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# In config/initializers/rack-attack.rb
class Rack::Attack
  # custom codes

  #for example, white list
  Rack::Attack.whitelist('allow from localhost') do |req|
    '127.0.0.1' == req.ip
  end

  #for example, black list
  Rack::Attack.blacklist('block 1.2.3.4.') do |req|
    '1.2.3.4' == req.ip
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;About rack-attack, I will give more details in next post.&lt;/p&gt;</description>
      <author>763914974</author>
      <pubDate>Mon, 21 Sep 2015 12:55:06 +0800</pubDate>
      <link>https://ruby-china.org/topics/27418</link>
      <guid>https://ruby-china.org/topics/27418</guid>
    </item>
    <item>
      <title>about html5</title>
      <description>&lt;p&gt;不知道目前有没有 html5 的大型资讯门户网站？  &lt;/p&gt;</description>
      <author>763914974</author>
      <pubDate>Thu, 05 Jul 2012 22:45:31 +0800</pubDate>
      <link>https://ruby-china.org/topics/4152</link>
      <guid>https://ruby-china.org/topics/4152</guid>
    </item>
  </channel>
</rss>
