LoginSignup
0
0

More than 5 years have passed since last update.

VIEW側にて文字成形をしないためにApplication_helper側に文字整形メソッドを持つ

Posted at

VIEW側でいちいち文字整形をしないために、統一的な文字整形メソッドをapplication_helperに持つ。

■ html_view_values メソッド

application_helper.rb

  def html_view_values (formtype, value, keyword="", delim=VIEW_DELIM_COMMA)
    html = ""

    if !value.nil? && (value.kind_of?(Array) || value.kind_of?(Hash))
      htmls = Array.new
      value.each do |key, val|
        if value.kind_of?(Array)
          htmls << view_values(formtype, key)
        else
          htmls << view_values(formtype, val, key)
        end
      end

      case delim
      when VIEW_DELIM_COMMA
        html = htmls.join(",")
      when VIEW_DELIM_HTML_UL
        html = "<ul>"
        htmls.each do |val|
          if val.present?
            html += "<li>"+val.to_s+"</li>"
          end
        end
        html += "<ul>"

        if html.is_a?(String) && keyword != ""
          html = highlight(html, keyword)
        end  
      end
    else
      html = view_values(formtype, value)
      if html.is_a?(String) && keyword != ""
        html = highlight(html, keyword)
      end  
    end
    return html
  end

■VIEW側の実装

interview_sheet.html.erb

<ul>
              <li><%= raw(html_view_values("interview_sheet_form.onset_pattern", @onset_pattern, "未回答")) %></li>
            </ul>

※先輩エンジニアがこうやって修正していたのでとりあえず備忘録代わりに。でも、メソッドの引数としての keyword="", delim=VIEW_DELIM_COMMA あたりが理解できていない。

0
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
0
0