我試圖在我的rails應用程序中創建一個搜索表單。我查了各種解決方案,但他們對我來說毫無意義。學習在Rails中搜索
當我在導軌應用程序中通過窗體運行搜索時,出現以下錯誤。現在我的關注點(除了錯誤)是我的索引操作中的實例變量@computers。我很確定這不是'軌道'來正確完成搜索,並會喜歡一些建議。
錯誤
undefined method `%' for #<Array:0x5780460>
參數搜索
http://localhost:3000/computers?utf8=%E2%9C%93&direction=&sort=&search=bob
搜索表單後
<%= form_tag computers_path, method: "get" do %>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Go", name: nil, class: "btn btn-primary" %>
<% end %>
呼叫的方法
def index
@computers = Computer.where(school_id: current_user.school_id).search(params[:search]).category(params[:category]).order(sort_column + " " + sort_direction)
end
方法
def Computer.search(search)
if search
search = search.downcase
params = []
values = {}
column_names.each do |c|
params << "#{c} LIKE #{c.to_sym}"
values[c.to_sym] = search
end
params.join (' OR ')
where(params,values)
else
all
end
end
發佈您的代碼。 –
什麼行會拋出該錯誤? –
其中(params,values),當我定義同樣的方法時,它在軌道控制檯中也發生了孤立。 – Corey