String is always html escaped in ‘config/locales/en.yml’

‘config/locales/en.yml’

en:
  hello_message: "Hello <strong>%{user_name}</strong>"

hello.html.erb

<%= t("hello_message", user_name: @user.name) %>

check hello.html in your brower. <strong></strong>tag is html escaped automatically.

hello <strong>Tomoyuki</strong>

html source is like this

hello &lt;strong&gt;Tomoyuki&lt;/strong&gt;

Output html and locales with html safe

You can set locale and set html tags in ‘conf/locales/XX.yml’ with html safe. Try to use ‘XXX_html’ suffix in your locale key.

en:
  hello_message_html: "Hello <strong>%{user_name}</strong>"

hello.html.erb

<%= t("hello_message", user_name: @user.name) %>

check hello.html in your brower. <strong></strong>tag is not html escaped automatically.

hello Tomoyuki

html source is like this

hello <strong>Tomoyuki</strong>

Reference

  • http://guides.rubyonrails.org/i18n.html#using-safe-html-translations

You can check loal definition using defined? and local_assigns.has_key? local_assigns.has_key? is more simple than defined?. You can write one line.

<% if defined? :user_name %>
 <p>hello <%= user_name  %></p>
<% else %>
 <p>hello world</p>
<% end %>

OR

<p>hello <%= local_assigns.has_key? :user_name ? user_name : "world" %></p>

Time object in ruby can not parse Time string(e.g. 2015-05-29T19:59:48+09:00) by default.

# return Time objet with "2015-01-01 00:00:00 +0900"
Time.new("2015-05-29T19:59:48+09:00")

You need to require time to extend default time.

# return Time objet with "2015-05-29T19:59:48+09:00"
Time.parse("2015-05-29T19:59:48+09:00")

Kaminari is popular pagination gem.

https://github.com/amatsuda/kaminari

Basic Usage

prepare target

@articles = Articles.page(params[:page])
# or
@articles = Articles.where(name: params[:name]).page(params[:page])
# or set per page
@articles = Articles.where(name: params[:name]).page(params[:page]).per(5) # 5 items each in list

render pagination

<%= paginate @articles %>

Config

generate config file

rails g kaminari:config
default_per_page  # 25 by default
max_per_page      # nil by default
max_pages         # nil by default
window            # 4 by default
outer_window      # 0 by default
left              # 0 by default
right             # 0 by default
page_method_name  # :page by default
param_name        # :page by default

Page scope method

http://www.rubydoc.info/github/amatsuda/kaminari/master/Kaminari/PageScopeMethods

You can access some value about pagination in erb

example:

<%= @articles.first_page? %>
<%= @articles.last_page? %>
<%= @articles.current_page? %>

Customize pagination

make pagination using default template

app/views/kaminari/xxxx.erb

rails g kaminari:views bootstrap

# generate template in `app/views/kaminari/xxxx.erb`

mkdir app/views/kaminari/${theme_name}
cp app/views/kaminari/*.erb app/views/kaminari/${theme_name}/

# edit template

You can use this template like this.

<%= paginate @articles, theme: "#{theme_name}" %>

Basic Usage

You can set style value using scope.

<div image class="image" ng-style="{'background-image': 'url(' + backimg + ')'}">
$scope.backimg = 'XXXXX' // image url

You can switch style value using expression.

<div image class="image" ng-style="{ 'width': isMobile: ? '200px': '300px' }">
$scope.isMobile = true // switch value to change style

Customize Usage

How to change style propertry and value using expression

<div image class="image" ng-style="getStyle(ua)">
$scope.getStyle = function(ua){
  if(ua.match(/Android/i)){
    return { display: 'none' };
  }else{
    return { width: '300px': height: '300px' };
  }
}