Note that there are some explanatory texts on larger screens.

plurals
  1. POError in utf8 encoding while creating object. Normal when update
    text
    copied!<p>Sorry for my bad english. I have a very strange bug. When I create an instance of a <em>Task</em> model with utf8 content, the stored <em>content</em> in database is not what it was input. The <em>content</em> field is created using ckeditor. Here it is:</p> <ul> <li>What user input: <code>Lưu</code></li> <li>The params: <code>{"task"=&gt; {"name"=&gt;"Store", "description"=&gt;"Store", "price"=&gt;"10000", "content"=&gt;"&lt;p&gt;\r\n\tLưu&lt;/p&gt;\r\n"}, "commit"=&gt;"Create Task", "action"=&gt;"create", "controller"=&gt;"tasks"}</code></li> <li>The instance in memory: <code>#&lt;Task id: nil, name: "Store", description: "Store", price: 10000, created_at: nil, updated_at: nil, content: "&lt;p&gt;\r\n\tLưu&lt;/p&gt;\r\n"&gt;</code></li> <li>What is stored: <code>#&lt;Task id: 10, name: "Store", description: "Store", price: 10000, created_at: "2012-05-22 03:47:11", updated_at: "2012-05-22 03:47:11", content: "\\x3c703e0d0a094cc6b0753c2f703e0d0a"&gt;</code></li> </ul> <p>However, the strangest part is that when I edit that model and save it, the content is stored as it is inputed.</p> <p>Here is my migration for the content column:</p> <pre><code>class AddContentToTasks &lt; ActiveRecord::Migration def change add_column :tasks, :content, :text end end </code></pre> <p>Here is my controller:</p> <pre><code> def update @task.update_attributes!(params[:task]) end def create binding.pry @task = Task.new(params[:task]) @task.user = current_user @task.save redirect_to tasks_path end </code></pre> <p>When I debug (using binding.pry), I see that the @task.content is correct. However, after @task.save, the last created Task's content (Task.last) is not.</p> <p>UPDATE 1: My database.yml</p> <pre><code>development: adapter: postgresql encoding: utf8 reconnect: false database: crowd pool: 5 username: username password: password host: localhost test: &amp;test adapter: postgresql encoding: utf8 reconnect: false database: crowd-test pool: 5 username: username password: password host: localhost </code></pre> <p>My schema.rb content.</p> <pre><code># encoding: UTF-8 ActiveRecord::Schema.define(:version =&gt; 20120519133158) do create_table "tasks", :force =&gt; true do |t| t.string "name" t.string "description" t.integer "price" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false t.integer "user_id" t.integer "task_type_id" t.text "content" t.date "expired_at" end end </code></pre> <p>My schema info from postgre</p> <pre><code> List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges ------------+-----------+----------+-------------+-------------+----------------------- crowd | username | UTF8 | en_US.UTF-8 | en_US.UTF-8 | crowd-test | username | UTF8 | en_US.UTF-8 | en_US.UTF-8 | </code></pre> <p>UPDATE 2: Instead of using <code>@task = Task.new(params[:task])</code>, I set all the attribute of the @task instance by myself. The stored content is also correct.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload