儘管我之前曾問過這個問題,但尚未解決。 我有一個名爲NAME,SCHOOLNAME,PARENTNAME等列的學生模型。 我現在想要做的是在用戶可以搜索以下列名稱時進行搜索...... 例如:如果用戶提出NAME在搜索欄中,然後他應該得到學生的所有名稱,如果用戶搜索學期名稱名稱,那麼他應該得到所有學校的名稱.... 我嘗試了很多,但沒有達到我想要的 幫助將不勝感激...謝謝在導軌中按列名搜索
對不起,沒有以適當的方式添加此問題。
在我的項目加入@fbelanger解決方案之後,居然什麼也沒發生什麼我尋找它給我同樣的結果...
我控制器
class DataController < ApplicationController
before_action :set_datum, only: [:show, :edit, :update, :destroy]
# GET /data
# GET /data.json
def index
@data = Datum.all
query = params[:query]
Datum.all.map(&:query) if Datum::QUERIABLE.include?(query)
end
# GET /data/1
# GET /data/1.json
def show
end
# GET /data/new
def new
@datum = Datum.new
end
# GET /data/1/edit
def edit
end
# POST /data
# POST /data.json
def create
@datum = Datum.new(datum_params)
respond_to do |format|
if @datum.save
format.html { redirect_to @datum, notice: 'Datum was successfully created.' }
format.json { render :show, status: :created, location: @datum }
else
format.html { render :new }
format.json { render json: @datum.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /data/1
# PATCH/PUT /data/1.json
def update
respond_to do |format|
if @datum.update(datum_params)
format.html { redirect_to @datum, notice: 'Datum was successfully updated.' }
format.json { render :show, status: :ok, location: @datum }
else
format.html { render :edit }
format.json { render json: @datum.errors, status: :unprocessable_entity }
end
end
end
# DELETE /data/1
# DELETE /data/1.json
def destroy
@datum.destroy
respond_to do |format|
format.html { redirect_to data_url, notice: 'Datum was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_datum
@datum = Datum.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def datum_params
params.require(:datum).permit(:name, :school, :hospital, :city, :food)
end
end
我的模型
class Datum < ActiveRecord::Base
QUERIABLE = %w(name city)
end
我的索引
<p id="notice"><%= notice %></p>
<h1>Listing Data</h1>
<%= form_tag(data_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search here" %>
<%= submit_tag "Search" %>
<% end %>
<table>
<thead>
<tr>
<th>Name</th>
<th>School</th>
<th>Hospital</th>
<th>City</th>
<th>Food</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @data.each do |datum| %>
<tr>
<td><%= datum.name %></td>
<td><%= datum.school %></td>
<td><%= datum.hospital %></td>
<td><%= datum.city %></td>
<td><%= datum.food %></td>
<td><%= link_to 'Show', datum %></td>
<td><%= link_to 'Edit', edit_datum_path(datum) %></td>
<td><%= link_to 'Destroy', datum, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Datum', new_datum_path %>
我記得我之前回答過。這不行嗎? http://stackoverflow.com/questions/40955019/advance-table-search-in-rails/40966091#40966091 –
^哇同一個人,同樣的問題,同樣的答案。 – fbelanger