【问题标题】:ruby on rails best_in_place gem field name change on error displayruby on rails best_in_place gem 字段名称在错误显示时更改
【发布时间】:2012-08-19 19:59:37
【问题描述】:

所以我已经设置了 best_in_place gem [https://github.com/bernat/best_in_place],但是当我尝试编辑的列看起来像 title_name 并且发生错误时,我遇到了一个小问题 p>

'title_name can't be blank,is too short (minimum is 1 characters)'

我想编辑 gem 处理错误显示的方式,以便它将 _ 替换为 space 并可能稍微改一下 user-friendly

编辑:

检查响应我通过 Firebug 得到的是:

{"title_name":["can't be blank","is too short (minimum is 1 characters)"]}

在 gem 演示中的位置 http://bipapp.heroku.com/users/59

["Last name has invalid length","Last name can't be blank"]

这有点不同...

EDIT2:

post.rb

class Post < ActiveRecord::Base
  attr_accessible :post_id, :title_name, :total_items, :user_id

  validates :title_name, :presence => true, :length => { :in => 1..50 }

  belongs_to :user, :foreign_key => 'post_id'

  self.primary_key = :post_id

  def to_param
    "#{post_id}"
  end
end

【问题讨论】:

  • @rudolph9 我找不到任何可以使用的选项来更改通过 gem api 显示错误的方式,这就是为什么我在这里发布它,猜测有人可能以前遇到过同样的问题......
  • 这是一个有趣的问题。查看example app,如果您为last_name 输入一个 字符,则会返回错误“姓氏长度无效”,其中表中的条目为last_name。跨度>
  • 继续克隆示例并将last_name 折射为last_curseword,应用程序现在会生成输出“Last curseword has invalid length”。
  • 我也在 github 上搜索代码,但在使用 gem 的方式上我找不到任何与我的应用程序不同的东西......仍在搜索
  • 我已经用我在 ajax 响应中发现的差异编辑了这个问题......这可能是问题,但我不确定......

标签: ruby-on-rails error-handling best-in-place


【解决方案1】:

解决方案:

我发现best_in_place gem在保存未完成时使用如下方法显示错误

respond_with_bip(@user

作为

附加到format.json
format.json { respond_with_bip(@user) }

所以用这段代码替换我通过 json 显示错误的方式解决了问题。

【讨论】:

  • 不错!这是他们在示例应用程序中默认更改它的方式吗?
  • 它是 rails 函数还是 gem 中包含的东西?
  • 我不确定...我使用 sublime find in folder 选项搜索了它,但找不到它...我猜它在 gem 内
【解决方案2】:

这其实很简单,与best_in_pace gem 无关。

修改文件config/locales/en.yml

en:
  activerecord:
    models:
      attributes:
        post:
          title_name: 'Title name'

现在属性title_name 应该在您的视图中替换为字符串'Title name'

注意:上面列出的方法会覆盖默认值,它没有设置默认值。

【讨论】:

  • 因为我的模型是 Post 并且列名是 title_name 不应该 userpostlast_nametitle_name 在上面的代码中?我试过了,但它似乎仍然不起作用......在 github 的代码示例中,语言环境中也没有任何内容
  • 你能仔细检查一下吗?我修改了我之前的内容(忘记更改一些东西)......并且文件 is 确实存在于the example included with the gem
猜你喜欢
  • 1970-01-01
  • 2017-06-15
  • 2012-12-16
  • 1970-01-01
  • 2012-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多