照着 rails guides 做 blog 的练习,我想让页面提示“名字不能为空!!!”怎么设置?
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title
validates_acceptance_of :name,message: '名字不能为空!!!!'
end
现在页面提示:Name 名字不能为空!!!! 看李刚的 ruby on rails 敏捷开发里,说是在 html 文件里 写 map{name => 姓名} error_message_for post,map 就可以
试了不管用,谁知道咋弄?
#2 楼 @qichunren李刚那个书是一开始刚学时买的,我主要是喜欢他讲东西中式化,别人的很多书虽然翻译了,但是是西式思想看着累。。。。。
#3 楼 @themorecolor 看到你这个我还想问你个问题,你的 yml 文件是中文的,我也想那么做,但是按着网上的做了,取不到 yml 文件里的汉字 /blog/config/application.rb
config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = "zh-CN"
你是怎么设置的,都安装了什么?多谢。
#zh-CN.yml
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
"zh-CN":
Sign in: 登 陆
Remeber me: 记住我
Logout: 退出
Login: 登陆
Sign up: 注册
Password confirmation: 确认密码
Sign in with: 使用#{provder}账号登陆
Forgot your password?: 忘记密码?
New website: 添加网址收藏
submit: 确定
cancel: 取消
member: 会员
activerecord:
models:
user: 用户
user_website: 网址
attributes:
user:
username: 用户名
email: 邮箱
password: 密码
password_confirmation: 确认密码
user_website:
name: 网址名称
url: 网址url
user_category_id: 网址分类
is_public: 是否公开
errors:
models:
user:
attributes:
username:
blank: "用户名不能为空!"
devise:
sessions:
user:
signed_in: 登陆成功
signed_out: 退出成功
helpers:
submit:
create: "创建 %{model}"
update: "Confirm changes to %{model}"
#application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Okeyso
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable escaping HTML in JSON.
config.active_support.escape_html_entities_in_json = true
# Use SQL instead of Active Record's schema dumper when creating the database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql
# Enforce whitelist mode for mass assignment.
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
config.active_record.whitelist_attributes = true
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.i18n.default_locale = "zh-CN"
end
end
view 里面,处理 errors 的部分,不用 full_messages,
这样做,errors.each do |name, msg|
,那么,msg 就是'名字不能为空!!!!',name 是属性名……