在Rails中使用嵌套模型进行表单验证

时间:2016-01-22 16:43:33

标签: ruby-on-rails forms validation

我有这个问题。我需要在Rails中以相同的形式验证两个模型的属性。一个是另一个的父母。

表格是这样的:

class User < ActiveRecord::Base
  belongs_to :role, polymorphic: true
  validates  :email, :password, presence: true

end

我使用的模型是这样的:

用户:

class Professional < ActiveRecord::Base
 has_one :user, as: :role, dependent: :destroy

 accepts_nested_attributes_for :user

   validates  :date_birthday, :gender, :height, :name, :description, :Weight, :address, :languages,:services, :category, :phonenumber, :fullname, :hair_color, :age, :orientation, presence: true

end

人:

create table #text_table (text_field varchar(250))


insert into #text_table values 
('Scheme ELS updated. Reserve Authority Level changed from: 11000000.00 to: 8000000.00.'),      
('Scheme CNST updated. Payment Authority Level changed from: 8000000.00 to: 10000000.00. Reserve Authority Level changed from: 8000000.00 to: 10000000.00.'),
('Scheme ELS updated. Payment Authority Level changed from: 350000.00 to: 100000.00.')

drop table #text_table2
select *, 
case when charindex('Reserve', text_field ,0) > 0 and charindex('Payment', text_field,0) = 0 then 'Reserve'  
     when charindex('Payment', text_field,0) > 0 and charindex('Reserve', text_field,0) = 0 then 'Payment' 
     when charindex('Payment', text_field,0) > 0 and charindex('Reserve', text_field,0) > 0 then 'Both'
     else 'N/A' end as tag,
charindex('from: ',text_field,0) as From_Index,
charindex(' to: ',text_field, charindex('from: ',text_field,0)) as to_index,
charindex('.',text_field, charindex(' to: ',text_field, charindex('from: ',text_field,0))) as dot_index
into #text_table2
from #text_table

select * 
into #simple
from #text_table2 where tag in ('Reserve','Payment')

select
substring(text_field, 0, charindex('.00. ',text_field,0)+3) as text_field
into #parse
from #text_table2 where tag = 'Both'
union
select
substring(text_field, charindex('.00. ',text_field,0)+5, charindex('.00.',text_field,charindex('.00.',text_field,0)+3)) 
from #text_table2 where tag = 'Both'

insert into #simple
select *, 
case when charindex('Reserve', text_field ,0) > 0 and charindex('Payment', text_field,0) = 0 then 'Reserve'  
     when charindex('Payment', text_field,0) > 0 and charindex('Reserve', text_field,0) = 0 then 'Payment' 
     when charindex('Payment', text_field,0) > 0 and charindex('Reserve', text_field,0) > 0 then 'Both'
     else 'N/A' end as tag,
charindex('from: ',text_field,0) as From_Index,
charindex(' to: ',text_field, charindex('from: ',text_field,0)) as to_index,
charindex('.',text_field, charindex(' to: ',text_field, charindex('from: ',text_field,0))) as dot_index
from #parse




select *,
substring(text_field, from_index+6, to_index - from_index - 6) as From_Value,
substring(text_field, to_index+4, dot_index - to_index - 1) as To_Value
from #simple

那么,问题是什么?

当我点击提交按钮时,会标记专业属性,但不会标记用户属性。

像这样:

enter image description here

以红色标记的字段属于专业模型,但是电子邮件和密码属于用户模型的字段在应该是红色时不会标记为红色,因为它们是空的。

我该怎么办?我需要用户的警告信息也是属性

感谢您的进步。

2 个答案:

答案 0 :(得分:1)

我们之前已经实现了你所需要的。

我们必须使用inverse_of,以便对象是一个单独的数据(而不是默认情况下的多个部分):

#app/models/user.rb
class User < ActiveRecord::Base
  belongs_to :role, polymorphic: true, inverse_of: :user
  validates  :email, :password, presence: true
end

#app/models/professional.rb
class Professional < ActiveRecord::Base
  has_one :user, as: :role, dependent: :destroy, inverse_of: :role
  accepts_nested_attributes_for :user
end

This会有所帮助。

您还需要确保正确传递这些对象(我看到很多人没有这样做)。

答案 1 :(得分:0)

您需要告诉专业人员验证相关用户:

class Professional < ActiveRecord::Base
    ... 
    validates_associated :user