我需要一些幫助將搜索字段放在我的視圖上。在聯合表中搜索
我買了一張家庭桌子,每個家庭都有0-n本書。我想搜索只顯示特定書籍的家庭。
現在我使用洗劫其他一些簡單的搜索:
的Controler:
class FamiliesController < ApplicationController
def index
@search = Family.search(params[:q])
@families = @search.result
respond_to do |format|
format.html # index.html.erb
format.json { render json: @families }
end
end
型號:
class Family < ActiveRecord::Base
has_many :names
has_and_belongs_to_many :books, :join_table => "books_families"
has_and_belongs_to_many :races, :join_table => "families_races"
attr_accessible :descr, :nome, :book_ids, :race_ids
validates :nome, uniqueness: true, presence: true
end
查看:
<fieldset>
<legend>Filter for families</legend>
<%= search_form_for @search do |f| %>
<div class="field">
<%= f.label :nome, "Name: " %><br />
<%= f.text_field :nome_cont, :class => "search-field"%>
</div>
<div class="actions">
<%= f.submit "Search" %>
</div>
<% end %>
</fieldset>
<table>
<tr>
<th><%= sort_link(@search, :nome, "Name") %></th>
<th>Books</th>
<th>Descr</th>
</tr>
(...)
<td>
<ol type="disc">
<% family.books.each do |book| %>
<li> <%= book.nome %> </li>
<% end %>
</ol>
</td>
(...)
我應該怎麼做?
因此,您希望用戶搜索一本書,並且您的代碼返回與該書關聯的所有家庭,對嗎? – Arvoreniad
是的,那我需要 – Techmago
我的答案是否解決您的問題? – Arvoreniad