我正在做一些简单的应用程序,我不会成为某种翻译应用程序,所以我有一个搜索选项和添加翻译选项。我的搜索有问题。当我点击搜索时没有任何单词,它显示我有一个翻译列表。我希望那里有一个 flash 错误,就像我有它一样,当我寻找一个不在数据库中的单词时。
我的模型
class Translation < ActiveRecord::Base
attr_accessible :text_english, :text_polish
validates_presence_of :text_english, :text_polish
validates :text_polish, :text_english, :uniqueness => true
def self.search(search)
if search
where("text_english LIKE ? OR text_polish LIKE ?", "%#{search.strip}%", "%#{search.strip}%")
else
scooped
end
end
end
在我的控制器中
# GET /translations/search
def search
@translations = Translation.search(params[:search])
if !@translations.empty?
respond_to do |format|
format.html
end
else
flash[:error] = "NO TRANSLATIONS"
redirect_to new_translation_url
end
end
end
和我在索引中的搜索表单
<h1>Words</h1>
<%= form_tag search_translations_path, method: :get do %>
<div class="field">
<%= text_field_tag :search, params[:search] %>
<button type="submit" class="btn">Search</button>
<% end %>
感谢任何帮助。