我的RoR应用程序中有一个工作搜索表单但是它不识别大写字母。因此,如果我搜索某些内容,则必须区分大小写。我尝试在搜索方法中添加UPPER但是它会引发错误。如何让它不区分大小写?
这是控制器:
def index
if params[:search]
@restaurants = Restaurant.search(params[:search]).order("created_at DESC")
else
@restaurants = Restaurant.joins("LEFT JOIN reviews ON restaurants.id = reviews.restaurant_id").group("restaurants.id").order("AVG(reviews.rating) DESC NULLS LAST")
end
end
这是模型中的方法:
def self.search(query)
where("name LIKE ?", "%#{query}%")
end
最后是表格:
<section class="search-form">
<%= form_tag(restaurants_path, :method => "get", id: "search-form", class: "navbar-form navbar-left") do %>
<div class="form-group">
<%= text_field_tag :search, params[:search], placeholder: "Search restaurants", :autocomplete => :off, class: "form-control", id: "search-field", required: "required" %>
</div>
<%= submit_tag "Search", :name => nil, id: "search-button", class: "btn btn-default" %>
<% end %>
</section>
非常感谢任何建议。谢谢。
答案 0 :(得分:0)
您始终可以将params[:search]
转换为小写,我可以执行以下操作:
Restaurant.search(params_search).order("created_at DESC")
private
def params_search
params[:search].to_s.downcase
end