1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【RubyonRails入門】独自のヘルパーを作成する

Last updated at Posted at 2019-09-10

[Rails]独自のヘルパーをつくる

1.ヘルパー

app/helpers/forms_helper.rb
module FormsHelper
    def hello(name)
        "こんにちは、" + name + "さん。"
    end
    
    def check_age(age)
        if age.to_i >= 15
            "ようこそ、お楽しみください。"
        else
            "このサイトは15歳以上限定です。"
        end
    end
end

■to_i と to_s

.to_i ==> int型(数字)に変換する
.to_s ==> string型(文字)に変換する

2.ヘルパーを元にした、ビュー

app/views/forms/formtest.html.erb
<%= hello("花子") %>
<!-- こんにちは、花子さん。 -->
app/views/forms/input_age.html.erb
<%= check_age(25) %>
<!-- ようこそ、お楽しみください。 -->

<%= check_age(25) %>
<!-- このサイトは15歳以上限定です。 -->
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?