Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何将来自两个不同模型的数据合并到rails中的一个选项中_Jquery_Ruby On Rails - Fatal编程技术网

Jquery 如何将来自两个不同模型的数据合并到rails中的一个选项中

Jquery 如何将来自两个不同模型的数据合并到rails中的一个选项中,jquery,ruby-on-rails,Jquery,Ruby On Rails,我有一个图书馆应用程序,一个人可以借一本书,无论是老师还是学生。在我的借阅表单中,两个模型都有自己的选择,我只希望两个模型都有一个选择,并通过另一个虚拟选择控制,该虚拟选择有两个选项教师和学生,通过选择其中一个,它将从一个表中动态加载数据 <%= form_for(@borrow) do |f| %> <% if @borrow.errors.any? %> <div id="error_explanation"> <h2&g

我有一个图书馆应用程序,一个人可以借一本书,无论是老师还是学生。在我的借阅表单中,两个模型都有自己的选择,我只希望两个模型都有一个选择,并通过另一个虚拟选择控制,该虚拟选择有两个选项教师和学生,通过选择其中一个,它将从一个表中动态加载数据

  <%= form_for(@borrow) do |f| %>
  <% if @borrow.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@borrow.errors.count, "error") %> prohibited this borrow from being saved:</h2>

      <ul>
      <% @borrow.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :book_issue_date %><br>
    <%= f.date_select :book_issue_date %>
  </div>
  <div class="field">
    <%= f.label :book_return_date %><br>
    <%= f.date_select :book_return_date %>
  </div>

  <div class="field">
    <%= f.label :book_id %><br>
    <%= f.collection_select(:book_id, Book.all, :id, :book_title) %>
  </div>

  <div class="field">
    <%= f.label :student_id %><br>
     <%= f.collection_select(:student_id, Student.all, :id, :student_name) %>
  </div>
  <div class="field">
    <%= f.label :teacher_id %><br>
   <%= f.collection_select(:teacher_id, Teacher.all, :id, :teacher_name) %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

禁止保存此借用:






使用多态关系如下

class Book < ActiveRecord::Base
  belongs_to :borrowable, polymorphic: true
end 

class Teacher < ActiveRecord::Base
  has_many :books, as: :borrowable
end

class Students < ActiveRecord::Base
  has_many :books, as: :borrowable
end

将给您教师或学生,您可以在书中有其他属性,即书的发行日期、书的归还日期等

我在借阅表单中必须进行的自定义。@MuhammadRafique您必须根据我上面的解释更改书、教师和学生中的模型关联OK,但是借款的形式也需要根据新的协会进行修改。我在询问借阅表格中需要做哪些更改。现在,在借阅表格中,学生和教师都可以选择他们的收藏。
@book.borrowable_type