2015-10-19 32 views
0

我有一個圖書館應用程序,在這個應用程序中,一個人可以借一本書借給老師或學生。在我的借​​用表格中,模型都有他們的選擇我只想要一個選擇,並通過另一個虛擬選擇來控制它,這個虛擬選擇有兩個選項,老師和學生通過選擇其中一個選項,從一個表動態地加載數據。如何結合來自兩種不同模型的數據在軌道中的一個選擇中顯示

<%= 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 %> 

screen shot of borrow new page

回答

0

如下

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 

有現在你的書單選擇

@book.borrowable_type 

會給你的老師或學生使用多態的關係,你可以有其他書中的屬性即book_issue_date,book_return_date等

+0

我需要借用什麼樣的定製方式。 –

+0

@MuhammadRafique你必須根據我上面解釋的 –

+0

在書籍,老師和學生中改變模特協會好吧,但是借閱的形式也需要根據新的協會進行更改。我在詢問借款表單中需要的更改。現在借用形式,學生和老師都選擇他們的收藏。 –

相關問題