学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

【学習5.0】【MacOSX】タイムスタンプ(timeago)

相対タイムスタンプを追加します。


「GemFile」に以下の記述を追加します。


記述追加 GemFIle(99行目)

gem 'rails-timeago', '~> 2.0'



GemFile

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.7', '>= 5.0.7.1'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

# bulma
gem 'bulma-rails', '~> 0.7.4'
gem 'bulma-extensions-rails', '~> 1.0.30'

# デバイス
gem 'devise', '~>4.2'

# アバター
gem 'gravtastic'

# toastr
gem 'toastr-rails', '~> 1.0'

# 日本語化
gem 'rails-i18n'

# google認証
gem 'omniauth'
gem 'omniauth-google-oauth2'

# 管理ダッシュボード
gem 'activeadmin'

# 管理ダッシュボードのテーマ
gem 'active_skin'

#画像アップロード
gem 'paperclip', '~> 5.1.0'

# アマゾンS3
gem 'aws-sdk', '~> 2.8'

# タスクの順番を変える
gem "active_admin-sortable_tree", "~> 2.0.0"

# Markdown関数
gem 'redcarpet', '~> 3.2.3'
gem 'coderay', '~> 1.1.0'

# Stripe決済
gem 'stripe', '~> 3.0.0'
gem 'rails-assets-card', source: 'https://rails-assets.org'

#タイムスタンプ
gem 'rails-timeago', '~> 2.0'



コマンド
bundle


「app\assets\javascripts\application.js」ファイルに以下の記述を追加します。


記述追加 app\assets\javascripts\application.js(18行目)

//= require rails-timeago



app\assets\javascripts\application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require toastr
//= require gravtastic
//= require card
//= require rails-timeago
//= require turbolinks
//= require_tree .



「app\views\reviews\_index.html.erb」ファイルの16行目の記述を以下の記述に置き換えます。


記述置き換え app\views\reviews\_index.html.erb(16行目)

<span class="right"><%= time_ago_in_words r.created_at, :nojs => true, :limit => 10.days.ago %></span>



app\views\reviews\_index.html.erb

<% if @reviews.blank? %>
	<span class="center"><h4>まだレビューはありません。</h4></span>
<% end %>

<% @reviews.order("id desc").each do |r| %>
	<hr>
	<div class="row">
		<div class="col m1">
        <figure class="image is-32x32">
            <%= image_tag r.user.gravatar_url, class: "is-rounded" %><br/>
            </figure>
		</div>
		<div class="col m10">
			<div>
                <strong><%= r.user.full_name %></strong>
                <span class="right"><%= time_ago_in_words r.created_at, :nojs => true, :limit => 10.days.ago %></span>
			</div>
			<div id="stars_<%= r.id %>"></div>
			<div><%= r.comment %></div>
            <br/>
			<div>
				<%= link_to "削除", [@project, r], class: "button is-danger", method: :delete, data: {confirm: "本当によろしいですか?"} if current_user && current_user.id == r.user.id %>
			</div>
		</div>
    </div>
    
	<script>
		$('#stars_<%= r.id %>').raty({
			path: '/assets',
			readOnly: true,
			score: <%= r.star %>
		});
	</script>
<% end %>



日本語化します。


「config\locales\ja.yml」ファイルに以下の記述を追加します。


記述追加 config\locales\ja.yml(14行目)

  datetime:
    distance_in_words:
      half_a_minute: "30秒前後前"
      less_than_x_seconds:
        one:   "1秒前"
        other: "%{count}秒前"
      x_seconds:
        one:   "1秒"
        other: "%{count}秒前"
      less_than_x_minutes:
        one:   "1分"
        other: "%{count}分前"
      x_minutes:
        one:   "約1分前"
        other: "%{count}分前"
      about_x_hours:
        one:   "約1時間前"
        other: "約%{count}時間前"
      x_days:
        one:   "1日前"
        other: "%{count}日前"
      about_x_months:
        one:   "約1ヶ月前"
        other: "約%{count}ヶ月前"
      x_months:
        one:   "1ヶ月前"
        other: "%{count}ヶ月前"
      almost_x_years:
        one:   "1年弱前"
        other: "%{count}年弱前"
      about_x_years:
        one:   "約1年前"
        other: "約%{count}年前"
      over_x_years:
        one:   "1年以上前"
        other: "%{count}年以上前"



config\locales\ja.yml

ja:
  activerecord:
    attributes:
      user:
        full_name: 氏名
        current_password: パスワード
        password: パスワード
        email: メールアドレス

  time:
    formats:
        full_date: "%Y年%m月%d日 (%a)"

  datetime:
    distance_in_words:
      half_a_minute: "30秒前後前"
      less_than_x_seconds:
        one:   "1秒前"
        other: "%{count}秒前"
      x_seconds:
        one:   "1秒"
        other: "%{count}秒前"
      less_than_x_minutes:
        one:   "1分"
        other: "%{count}分前"
      x_minutes:
        one:   "約1分前"
        other: "%{count}分前"
      about_x_hours:
        one:   "約1時間前"
        other: "約%{count}時間前"
      x_days:
        one:   "1日前"
        other: "%{count}日前"
      about_x_months:
        one:   "約1ヶ月前"
        other: "約%{count}ヶ月前"
      x_months:
        one:   "1ヶ月前"
        other: "%{count}ヶ月前"
      almost_x_years:
        one:   "1年弱前"
        other: "%{count}年弱前"
      about_x_years:
        one:   "約1年前"
        other: "約%{count}年前"
      over_x_years:
        one:   "1年以上前"
        other: "%{count}年以上前"        
        



ブラウザ確認
http://localhost:3000/project/1


タイムスタンプが表示されるようになりました。

タイムスタンプ表示
タイムスタンプ表示


関連記事(外部サイト)