我试图在我的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