简体   繁体   English

如何在rails中的new.html.erb中查找和验证外键

[英]How to lookup and validate foreign keys in new.html.erb in rails

My data model is that a student is associated with a teacher. 我的数据模型是学生与教师相关联。 My student entry form will have a teacher username field, which I want to validate against an existing teacher record. 我的学生报名表将有一个教师用户名字段,我想根据现有的教师记录进行验证。 How will I accomplish this in the app/view/student/new.html.erb file? 我将如何在app / view / student / new.html.erb文件中完成此操作? Right now, I just have this: 现在,我只是这样:

<tr>
 <th>Teacher ID</th>
 <td><%= f.number_field :teacher_id %></td>
</tr>

I want to make this the :teacher_username and then validate against the teachers table. 我想把它作为:teacher_username,然后根据教师表进行验证。 if a match is found, I want to get the teachers.id field and pass it on for insert into the student table. 如果找到匹配项,我想获取teachers.id字段并将其传递以插入到学生表中。 If a match is not found, then I want to flag an error that no such teacher username exists. 如果找不到匹配项,那么我想标记一个错误,即没有这样的教师用户名。 How can I accomplish this. 我怎样才能做到这一点。 Any sample code would be helpful. 任何示例代码都会有所帮助。

The student model file has belongs_to :teacher in it. 学生模型文件中包含belongs_to:teacher。 The teacher model file has has_many :students in it. 教师模型文件中有has_many:学生。

As @Iceman suggested,a drop-down box is appropriate for your purpose. 正如@Iceman建议的那样, drop-down框适合您的目的。

In your /views/student/new.html.erb you can do this /views/student/new.html.erb您可以执行此操作

<tr>
 <th>Select Teacher</th>
 <td><%= f.select :teacher_id,Teacher.all.collect {|t| [ t.teacher_username, t.id ] },{ include_blank: true } %></td>
</tr>

This will ensure that only the records that are present in teachers table will be available here in the drop-down and simplifies the validation part which you are looking. 这将确保drop-down teachers table中仅显示teachers table中存在的records ,并简化您正在查看的validation部分。

I want to make this the :teacher_username and then validate against the teachers table. 我想把它作为:teacher_username,然后根据教师表进行验证。 if a match is found, I want to get the teachers.id field and pass it on for insert into the student table. 如果找到匹配项,我想获取teachers.id字段并将其传递以插入到学生表中。 If a match is not found, then I want to flag an error that no such teacher username exists. 如果找不到匹配项,那么我想标记一个错误,即没有这样的教师用户名。 How can I accomplish this. 我怎样才能做到这一点。 Any sample code would be helpful. 任何示例代码都会有所帮助。

You may need to look at ActiveRecord validations 您可能需要查看ActiveRecord验证

More specifically, I'd do somthing like this: 更具体地说,我会做这样的事情:

#app/models/teacher.rb
Class Teacher < ActiveRecord::Base
    vaidates :username, uniqueness: true
end

I mention "like" because you want to pass the details to your student table. 我提到“喜欢”,因为你想将细节传递给student表。 If you're doing this with accepts_nested_attributes_for , then it's okay; 如果你用accepts_nested_attributes_for这样做,那就没关系; however if you're not, then you may wish to divulge your process, so we can help you define it 但是,如果您不是,那么您可能希望透露您的流程,以便我们帮助您定义它

-- -

Update 更新

Having read your question more thoroughly, it seems you're trying to look up a teacher's username, then if it exists, pass the id back to the student table 在更彻底地阅读了您的问题后,您似乎正在尝试查找教师的用户名,如果存在,则将该id传回student

A drop down will probably be the best option for this, however if you wanted to look up the name, you'd have to call an action on the students controller to validate the username via ajax, like: drop down可能是最好的选择,但是如果你想查找名称,你必须在students控制器上调用一个动作来通过ajax验证用户名,例如:

#app/controllers/students_controller.rb
def check_teacher
    teacher = Teacher.find_by username: params[:userame]
end

But having read the question properly, I'd recommend the drop down 但是在正确阅读了问题后,我建议下拉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM