1

我有一个具有 [ :user_id, :activity_id, :date, ....]等属性的费用模型

关系是:

expense belongs_to user
expense belongs_to activity

我有以下验证。

validates :activity_id, uniqueness: scope: [:user_id, :date], message: "Expense for 'activity_name' on date '1/1/2012' is already entered"

如何获取activity_namedate进入错误消息?

4

2 回答 2

1

Another approach (non-inline) is:

In model code:

validates :activity_id, uniqueness: => {scope: [:user_id, :date], :message => "error is here"}

In view code:

  = form_for(@expense) do |f|
    - if @expense.errors.any?
      = pluralize(@expense.errors.count, "error")
        prohibited this link from being saved:

        %ul
          -@expense.errors.full_messages.each do |msg|
            - if msg[0,12] == "error is here"
              = "Expense for "+ params[:activity_name] + " on date " + params[:date] + " is already entered"

params[:activity_name] is the correct way to access the entered data.

于 2013-05-17T10:02:56.283 回答
0

你可以使用:message属性。这是内联方法。

validates :activity_id, :uniqueness => {:scope => [:user_id, :date], :message => "Expense for "+ activity_name + " on date " + date + " is already entered"}
于 2013-05-17T09:25:11.920 回答