方法
实例公共方法
param_encoding(action, param, encoding) 链接
为操作上的参数指定编码。如果未指定,默认为 UTF-8。
您可以使用以下方式指定二进制 (ASCII_8BIT) 参数:
class RepositoryController < ActionController::Base # This specifies that file_path is not UTF-8 and is instead ASCII_8BIT param_encoding :show, :file_path, Encoding::ASCII_8BIT def show @repo = Repository.find_by_filesystem_path params[:file_path] # params[:repo_name] remains UTF-8 encoded @repo_name = params[:repo_name] end def index @repositories = Repository.all end end
show 操作上的 file_path 参数将被编码为 ASCII-8BIT,但所有其他参数将保持 UTF-8 编码。这在应用程序必须处理数据但数据编码未知(例如文件系统数据)的情况下非常有用。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal/parameter_encoding.rb, line 79 def param_encoding(action, param, encoding) @_parameter_encodings[action.to_s][param.to_s] = encoding end
skip_parameter_encoding(action) 链接
指定给定操作的所有参数都应编码为 ASCII-8BIT(它“跳过”了 UTF-8 的默认编码)。
例如,控制器会像这样使用它:
class RepositoryController < ActionController::Base skip_parameter_encoding :show def show @repo = Repository.find_by_filesystem_path params[:file_path] # `repo_name` is guaranteed to be UTF-8, but was ASCII-8BIT, so # tag it as such @repo_name = params[:repo_name].force_encoding 'UTF-8' end def index @repositories = Repository.all end end
上面控制器中的 show 操作将把所有参数值编码为 ASCII-8BIT。这在应用程序必须处理数据但数据编码未知(例如文件系统数据)的情况下非常有用。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal/parameter_encoding.rb, line 50 def skip_parameter_encoding(action) @_parameter_encodings[action.to_s] = Hash.new { Encoding::ASCII_8BIT } end