如何在页面加载时停止显示Flash错误消息,直到在rails上单击ruby按钮之后为止? [英] How do I stop a flash error message from showing on page load until after button is clicked in ruby on rails?

查看:94
本文介绍了如何在页面加载时停止显示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



<我明白为什么它显示(很明显,当页面被访问时,搜索参数已经空白)。在这个flash消息上我可以使用ruby中的一些技巧来阻止它直到搜索按钮被点击,例如

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]

这篇关于如何在页面加载时停止显示Flash错误消息,直到在rails上单击ruby按钮之后为止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆