【发布时间】:2012-11-01 12:55:30
【问题描述】:
我正在关注 Ruby on Rails 教程 (http://ruby.railstutorial.org/chapters/a-demo-app#sec-modeling_demo_users) 并成功完成了第 2.2.1 节的练习。
现在当我尝试加载在 2.2.1 中创建的任何用户资源(例如 localhost:3000/users 和 localhost:3000/user/new)时遇到字符编码问题,这基本上是不合时宜的-box 代码生成:
C:\Users\Dennis\rails_projects\demo_app> rails 生成脚手架 User name:string email:string
我的环境:
- Windows 7 64 位
- ruby 1.9.3p194 (2012-04-20) [i386-mingw32]
- Rails 3.2.9
Rails 返回一个以:
开头的错误页面Encoding::InvalidByteSequenceError in Users#index
显示 C:/Users/Dennis/rails_projects/demo_app/app/views/layouts/application.html.erb 其中第 6 行出现:
incomplete "\x00" on UTF-16LE (in C:/Users/Dennis/rails_projects/demo_app/app/assets/javascripts/users.js.coffee)提取的源代码(第 6 行附近):
3: <head> 4: <title>DemoApp</title> 5: <%= stylesheet_link_tag "application", :media => "all" %> 6: <%= javascript_include_tag "application" %> 7: <%= csrf_meta_tags %> 8: </head> 9: <body>
users.js.coffee 的内容:
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
application.html.erb 的内容:
<!DOCTYPE html>
<html>
<head>
<title>DemoApp</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
index.html.erb 的内容:
<h1>Listing users</h1>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
【问题讨论】:
-
请出示您的
users.js.coffee -
更新为显示 users.js.coffee、application.html.erb 和 index.html.erb。
-
检查您的
Gemfile,如果您有任何名为execjs的gem,请更改其版本,例如,将此gem "execjs", "1.4.0"添加到您的Gemfile 并运行bundle install以安装gem。然后再次运行应用程序以检查它是否有效。如果您没有该 gem,请尝试将users.js.coffee重命名为users.js并再次运行。 -
谢谢!将 users.js.coffee 重命名为 users.js 有效。 Gemfile 不包含 execjs,虽然我确实尝试添加带有版本的 execjs,但没有好的结果。出了什么问题?这会禁用 CoffeeScript 吗?
-
我认为它与 windows 上的咖啡脚本有一些冲突。是的,这意味着您只能在
users.js中编写javascript 代码。我发布了答案,请接受我的答案,谢谢:)
标签: ruby-on-rails character-encoding railstutorial.org