Redmine > Internal error > Template::Error  from ASCII\-8BIT to UTF\-8\ の対処

# Redmine でInternal error

Redmine でバージョンアップ後、カスタムクエリの画面を表示するとInternal error と無慈悲に表示された。

<pre>
Internal error

An error occurred on the page you were trying to access.
If you continue to experience problems please contact your Redmine administrator for assistance.

If you are the Redmine administrator, check your log files for details about the error.

Back
</pre>


ログを見ろって事なので、redmine\htdocs\log\production.log　を確認
内容は以下
<pre>
Started GET "/redmine/projects/aaa/issues?query_id=93" for 127.0.0.1 at 2017-11-22 15:41:40 +0900
Processing by IssuesController#index as HTML
  Parameters: {"query_id"=>"93", "project_id"=>"aaa"}
  Current user: user (id=1)
  Rendered queries/_filters.html.erb (70.0ms)
  Rendered queries/_query_form.html.erb (71.0ms)
  Rendered issues/index.html.erb within layouts/base (78.0ms)
Completed 500 Internal Server Error in 289ms (ActiveRecord: 109.0ms)

ActionView::Template::Error ("\xE7" from ASCII-8BIT to UTF-8):
     9: $(document).ready(function(){
    10:   initFilters();
    11:   <% query.filters.each do |field, options| %>
    12:   addFilter("<%= field %>", <%= raw_json query.operator_for(field) %>, <%= raw_json query.values_for(field) %>);
    13:   <% end %>
    14: });
    15: <% end %>
  app/helpers/application_helper.rb:1244:in `raw_json'
  app/views/queries/_filters.html.erb:12:in `block (2 levels) in _app_views_queries__filters_html_erb__591453197_90781044'
  app/views/queries/_filters.html.erb:11:in `each'
  app/views/queries/_filters.html.erb:11:in `block in _app_views_queries__filters_html_erb__591453197_90781044'
  app/views/queries/_filters.html.erb:1:in `_app_views_queries__filters_html_erb__591453197_90781044'
  app/views/queries/_query_form.html.erb:10:in `_app_views_queries__query_form_html_erb__74269014_90713952'
  app/views/issues/index.html.erb:11:in `block in _app_views_issues_index_html_erb___412293273_90609240'
  app/views/issues/index.html.erb:10:in `_app_views_issues_index_html_erb___412293273_90609240'
  app/controllers/issues_controller.rb:51:in `block (2 levels) in index'
  app/controllers/issues_controller.rb:46:in `index'
  lib/redmine/sudo_mode.rb:63:in `sudo_mode'
</pre>


"\xE7" from ASCII-8BIT to UTF-8　でエラーになっている模様。

以下に同じ現象の方がいましたので参考にさせていただきました。
[Redmine カスタムクエリが500 ActionView::Template::Error \("XXX" from ASCII\-8BIT to UTF\-8\) になるときの対処方法 \- Qiita](https://qiita.com/toronya/items/145f8be1c69e26ad2b9b)


## 対応

### application_helper.rb

htdocs\app\helpers\application_helper.rb

```rb
  # Helper to render JSON in views
  def raw_json(arg)
    arg.to_json.to_s.gsub('/', '\/').html_safe
  end
```

を以下に変更（１行追加）

```rb
  # Helper to render JSON in views
  def raw_json(arg)
    arg = force_utf8_strings(arg)      # add
    arg.to_json.to_s.gsub('/', '\/').html_safe
  end
```

同ファイル htdocs\app\helpers\application_helper.rb の末尾、privateエリアに以下のようにforce_utf8_stringsを追加。
force_utf8_stringsは\htdocs\app\models\setting.rbに存在する関数をコピー

```rb

private
  def wiki_helper
  end
.
.
  def remove_double_quotes(identifier)
  end
.

  def force_utf8_strings(arg)
    if arg.is_a?(String)
      arg.dup.force_encoding('UTF-8')
    elsif arg.is_a?(Array)
      arg.map do |a|
        force_utf8_strings(a)
      end
    elsif arg.is_a?(Hash)
      arg = arg.dup
      arg.each do |k,v|
        arg[k] = force_utf8_strings(v)
      end
      arg
    else
      arg
    end
  end
```


### queries_helper.rb

上記までで一度実行します。
今度は違うエラーが出ました。

```
ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8):
    31:   <%= form_tag(_project_issues_path(@project, :format => 'csv'), :method => :get, :id => 'csv-export-form') do %>
    32:   <%= query_as_hidden_field_tags(@query) %>
    33:   <p>
    34:     <label><%= radio_button_tag 'c[]', '', true %> <%= l(:description_selected_columns) %></label><br />
    35:     <label><%= radio_button_tag 'c[]', 'all_inline' %> <%= l(:description_all_columns) %></label>
    36:   </p>
    37:   <p>
  app/views/issues/index.html.erb:34:in `block in _app_views_issues_index_html_erb___619688709_82056480'
  app/views/issues/index.html.erb:31:in `_app_views_issues_index_html_erb___619688709_82056480'
  app/controllers/issues_controller.rb:51:in `block (2 levels) in index'
  app/controllers/issues_controller.rb:46:in `index'
  lib/redmine/sudo_mode.rb:63:in `sudo_mode'
```


\htdocs\app\helpers\queries_helper.rb

```rb
  # Returns the query definition as hidden field tags
  def query_as_hidden_field_tags(query)
    tags = hidden_field_tag("set_filter", "1", :id => nil)

    if query.filters.present?
      query.filters.each do |field, filter|
        tags << hidden_field_tag("f[]", field, :id => nil)
        tags << hidden_field_tag("op[#{field}]", filter[:operator], :id => nil)
        filter[:values].each do |value|
          tags << hidden_field_tag("v[#{field}][]", value, :id => nil)
        end
      end
    else
      tags << hidden_field_tag("f[]", "", :id => nil)
    end
    query.columns.each do |column|
      tags << hidden_field_tag("c[]", column.name, :id => nil)
    end
    if query.totalable_names.present?
      query.totalable_names.each do |name|
        tags << hidden_field_tag("t[]", name, :id => nil)
      end
    end
    if query.group_by.present?
      tags << hidden_field_tag("group_by", query.group_by, :id => nil)
    end
    if query.sort_criteria.present?
      tags << hidden_field_tag("sort", query.sort_criteria.to_param, :id => nil)
    end

    tags = force_utf8_strings(tags)      # add 2017/11/22 sugasaki

    tags
  end

```


application_helper.rbと同様にqueries_helper.rbの末尾に以下を追加します。

```rb
  private

  def force_utf8_strings(arg)
    if arg.is_a?(String)
      arg.dup.force_encoding('UTF-8')
    elsif arg.is_a?(Array)
      arg.map do |a|
        force_utf8_strings(a)
      end
    elsif arg.is_a?(Hash)
      arg = arg.dup
      arg.each do |k,v|
        arg[k] = force_utf8_strings(v)
      end
      arg
    else
      arg
    end
  end

```



## まとめ
変更したファイルは以下です。

* \htdocs\app\helpers\application_helper.rb
* \htdocs\app\helpers\queries_helper.rb

上記のファイル＋\htdocs\app\models\setting.rbに
force_utf8_strings関数が存在する状態になりましたが、
これが問題となるのは次のバージョンUP時でしょうか。

それまで覚えておきましょう。