如何在页面加载时停止显示Flash错误消息,直到在rails上单击ruby按钮之后为止?
我试图找出一种方法来阻止此页面加载时显示的消息:
I'm trying to figure out a way to stop this flash message from showing on page load:
class SearchesController < ApplicationController
def index
@users = User.search params[:search]
@default_image = "/assets/default_avatar.jpg"
if @users.empty? || params[:search].blank?
flash[:error] = "Sorry no user(s) found!" if @users.empty?
flash[:error] = "Please give us something to search for!" if params[:search].blank?
render 'index'
end
end
end
I understand why it's showing (obviously when page is visited the search params is blank already). There must be some trick in ruby on rails that I can use on this flash message to stop it from firing until search button is clicked e.g.
flash[:error] = "Please give us something to search for!" if params[:search].blank? after_get
请注意,这是after_get组成的。
Bare in mind that was after_get was made up.
我确定有人在那里有答案。
I'm sure someone out there has the answer for this.
我在rails api中看到了after_commit,但没有例子说明如何在我的情况或甚至是我所需要的。
I saw after_commit in the rails api but no example of how to use it in my situation or even if it's what I need.
亲切的谴责
一个简单的解决方案是在搜索表单中添加一个隐藏字段,并检查该值是否存在,而不是实际的搜索字符串。
A simple solution would be to add a hidden field to the search form and check if that value is present instead of the actual search-string.
查看
<%= hidden_field_tag :searching, true %>
控制器
Controller
flash[:error] = "Please give us something to search for!" if params[:searching]