rails 应用程序助手不支持中文字符 [英] rails application helper didn't support chinese characters

查看:36
本文介绍了rails 应用程序助手不支持中文字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def top_cateogries_with_home(category_id=-1)
    concat(raw "<select name='category_id' id='category_id'>")
    container=[]
    for category in Category.top
      container.push([category.name,category.id])
    end
    concat(raw "<option value='-1' selected>首页</option>")
    concat(raw options_for_select(container, selected = nil))
    concat(raw "</select>")
  end

我写了一个辅助方法application_helper.rb,但是它会抛出问题,如果容器中文字符首页,如果我用数字或其他字符替换它们,没关系,

i write a helper method a application_helper.rb,but it will throw issue,if container chinese character 首页,if i replace them with number or other character, it's ok,

我可以使用带有中文字符的注释,但是如果我想在助手中使用它会抛出错误,也可以用ror很好地显示数据库中的中文数据,

i can use comment with chinese character,but if i want to use in helper it throw error, also in the database's chinse data can display with ror well,

希望有人能帮助我,谢谢

hope someone could help me,thanks

推荐答案

尝试将魔术注释 # coding: utf-8 添加到 application_helper.rb 的开头

Try adding the magic comment # coding: utf-8 to the beginning of your application_helper.rb

如果您在 Ruby 1.9 上运行,则需要它.你也可以把它添加到你所有的 .rb 文件

It is needed if you are running on Ruby 1.9. You may as well add it to all your .rb files

这篇关于rails 应用程序助手不支持中文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆