有多个父母的孩子是否可以根据他周末与谁住在一起而拥有不同的身份验证? (导轨3)
我有一个 Rails 3.1 项目,其中包含可重用的多层嵌套表单。
例如,老师有一个嵌套的存在。
class Teacher < ActiveRecord::Base
has_one :being
accepts_nested_attributes_for :being
end
参赛者也有一个嵌套的存在,
class Entrant < ActiveRecord::Base
has_one :being
accepts_nested_attributes_for :being
end
我希望能够验证生日(在存在模型中),但前提是存在的父级是参赛者。
在存在模型中:
attr_accessor :no_birthday
validates :birthday, :presence => {:message => 'Birthday is required to enter classes.'}, :unless => :no_birthday?
def no_birthday?
unless self.no_birthday
errors.add(:entrant, "Birthday is required.")
end
end
在表单中(仅限教师版本)
<%= hidden_field_tag 'no_birthday', "1" %>
并且,以防万一,在教师控制器中:
def create
...
@teacher = Teacher.new(params[:teacher])
@teacher.being.no_birthday = 1
...
end
不知何故,我离目标还很远。我可以让验证对每个人或任何人都有效。我觉得有一个简单的解决方案与我并行。有人知道那可能是什么吗?
I have a Rails 3.1 project with multi-level nested forms which are reused.
For instance, a Teacher has a nested Being.
class Teacher < ActiveRecord::Base
has_one :being
accepts_nested_attributes_for :being
end
An Entrant also has a nested Being
class Entrant < ActiveRecord::Base
has_one :being
accepts_nested_attributes_for :being
end
I would like to be able to validate birthdays (in the being model), but only if the parent of being is Entrant.
In the being model:
attr_accessor :no_birthday
validates :birthday, :presence => {:message => 'Birthday is required to enter classes.'}, :unless => :no_birthday?
def no_birthday?
unless self.no_birthday
errors.add(:entrant, "Birthday is required.")
end
end
In the form (teacher version only)
<%= hidden_field_tag 'no_birthday', "1" %>
And, just in case, in the teacher_controller:
def create
...
@teacher = Teacher.new(params[:teacher])
@teacher.being.no_birthday = 1
...
end
Somehow, I am very far from the mark. I can make the validation work for everybody, or nobody. I feel like there is a simple solution running parallel to me. Anybody know what that may be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,上述方法存在一些问题:
1)验证线。虽然看起来:除非是真实的事情,但事实似乎并非如此。重新格式化为 :if 语句(在正在模型中):
我还更改了模型中方法的名称,以更好地理解英语,将其作为 if 而不是 except。
2)我可以通过从所需的控制器设置访问器来解决问题。因此,一个父级的控制器会将访问器设置为 true,而另一个则不会。没有隐藏的表单字段怪异或我正在寻找的任何其他随机方法:
父级 #1 的创建控制器,例如:
另一个父级根本没有设置此访问器。
当然,我必须为正在存在的模型创建一个适当的访问器,否则整个事情都会出错:
现在,一切正常了。教师不需要生日,而参赛者需要。终于双方父母可以和睦相处了。
So, a few problems with the above approach:
1) the validation line. while it seems like :unless should be a real thing, it doesn't seem to be the case. reformat as an :if statement (in the being model):
I also changed the name of the method in the model to make better english sense as an if instead of unless.
2) I can solve the problem by setting an accessor from the required controllers. So, one parent's controller will set the accessor to true, and another will not. No hidden form fields weirdness or any of the other random approaches I was looking for:
parent #1's create controller, as an example:
the other parent will simply not have this accessor set at all.
Of course, I will have to create an appropriate accessor for the being model or the whole thing will error out:
And now, it all works. Teachers don't need birthdays and entrants do. Finally both parents can get along.