【问题标题】:Sinatra Mongoid String not valid UTF-8Sinatra Mongoid 字符串无效的 UTF-8
【发布时间】:2011-04-22 15:58:39
【问题描述】:

我写了这个小应用程序:

require 'rubygems'
require 'sinatra'
require 'bson'
require 'mongoid'

Mongoid.configure do |config|
  name = "articles"
  host = "localhost"
  config.master = Mongo::Connection.new.db(name)
  config.persist_in_safe_mode = false
end

class Article
  include Mongoid::Document

  field :title
  field :content
end

get '/' do
  @articles = Article.all
end

get '/show/:id' do
  @article = Article.find(params[:id])
end

get '/new' do
  haml :new
end

post '/create' do
  @article = Article.new(params['article'])
  if @article.save
    redirect '/'
  else
    redirect '/new'
  end  
end

当我发布内容为“Test d'un article en français”的文章时出现以下错误

BSON::InvalidStringEncoding at /create String not valid UTF-8 

我该如何解决这个错误?

谢谢

【问题讨论】:

  • 您检查过表单 html 页面及其编码/字符集设置吗?应该是 UTF-8。

标签: ruby sinatra mongoid


【解决方案1】:

这是 Ruby 1.9 和 Sinatra 的一个已知问题。等待 Sinatra 1.1 发布或使用来自 github 的 Sinatra edge 版本。

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 2013-12-22
    • 2010-11-28
    • 2011-07-21
    • 2013-04-07
    • 2020-10-13
    • 2018-06-04
    • 2012-05-02
    • 2020-03-07
    相关资源
    最近更新 更多