Book

Book

Advertisements

Rails 保存进数据库前用日本时间,存进后用UTC格式,读出再转换成日本时间

Controller

before_filter :get_post_by_id, only: [:show, :edit, :update, :destroy] 

def new
  @post = Post.new
end

def create
  @post = Post.new(params[:post])
  @post.publish_date = DateTime.parse(params[:post][:publish_date]) - 9.hours
  if @post.save
    # TODO
  end
end

def edit
  @post.publish_date = @post.publish_date.in_time_zone('Tokyo').strftime('%Y/%m/%d %H:%M:%S')
end

def update
  @post.title = params[:post][:title]
  @post.publish_date = DateTime.parse(params[:post][:publish_date]) - 9.hours
  if @post.save
    # TODO
  end
end

private

  def get_post_by_id
    @post = Post.find(params[:id])
  end

View

new:
value => (Time.now + 9.hours).strftime('%Y/%m/%d %H:%M:%S')

edit:
(do not set value)

Twitter Bootstrap 圆形大按钮

CSS

.btn-circle {
  width: 30px;
  height: 30px;
  text-align: center;
  padding: 6px 0;
  font-size: 12px;
  line-height: 1.428571429;
  border-radius: 15px;
}
.btn-circle.btn-lg {
  width: 50px;
  height: 50px;
  padding: 10px 16px;
  font-size: 18px;
  line-height: 1.33;
  border-radius: 25px;
}
.btn-circle.btn-xl {
  width: 200px;
  height: 200px;
  padding: 10px 16px;
  font-size: 24px;
  line-height: 1.33;
  border-radius: 165px;
}

HTML

Title

</div>

Bash base | Loop sample

A loop sample

#!/bin/sh

SHARD_FOLDER=shards
rm -rf $SHARD_FOLDER
mkdir -p $SHARD_FOLDER

for i in `seq 14`
do
    if [ $i -lt 10 ]; then
        i='0'$i
    fi

    cat $SHARD_FOLDER/* | awk 'NR>1' | while read name age
    do
        echo $i$name
        echo $i$age
    done
done

File content

name  age
a     1
b     2

Output

01a
011
01b
012
02a
021
02b
022
...

Send mail by Devise 3.4 on Rails 4.1

When I use Devise 2 and Rails 3, there wasn’t any problem in this way:

# /config/environments/production.rb 's setting

config.action_mailer.default_url_options = { :host => Settings.mailer.host }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default :charset => "utf-8"

config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: Settings.mailer.domain,
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV['GMAIL_USERNAME'],
  password: ENV['GMAIL_PASSWORD']
}

# /config/application.yml 's setting

production:
  <<: *defaults
  mailer:
    host: something.herokuapp.com
    domain: http://something.herokuapp.com

But after I changed to Devise 3.4 and Rails 4.1, it always failed.I have asked a question on StackOverflow, but no answer can help me.

So I read the Devise document again, and know that the key point is subdomain!

I changed the source to:

# /app/views/devise/mailer/reset_password_instructions.html.erb

<%= link_to 'Change password', edit_password_url(@resource, reset_password_token: @token, subdomain: Settings.subdomain) %>

# /config/environments/production.rb 's setting

config.action_mailer.default_url_options = { :host => Settings.host }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default :charset => "utf-8"

config.action_mailer.smtp_settings = {
  address: "smtp.mandrillapp.com",
  port: 25,
  domain: Settings.domain,
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["MANDRILL_USERNAME"],
  password: ENV["MANDRILL_API_KEY"]
}

# /config/application.yml 's setting

production:
  <<: *defaults
  host: herokuapp.com
  domain: herokuapp.com
  subdomain: something

It works!

Useful links