我可以防止AJAX调用后出现闪光消息吗?

时间:2022-11-11 23:39:24

I have a website that degrades gracefully for users without javascript. For a user without javascript, the controller will respond to a form submit with HTML. For a user with javascript, there will be an AJAX form submit. This is in the create method in the controller:

我有一个网站,它对没有javascript的用户进行了优雅的降级。对于没有javascript的用户,控制器将用HTML响应表单提交。对于具有javascript的用户,将会有一个AJAX表单提交。这是在控制器的create方法中:

    if !@goal.save
      flash[:error] = array_to_list(@goal.errors.full_messages)
    else
      flash[:success] = "Your goal was successfully added."
    end      

    respond_to do |format|
        format.html { redirect_to :action => "show"}
        format.js
    end

I don't want to put those messages into flash if the user has javascript enabled, because then it will display after the user has already dealt with it. Is there any way to tell Rails to display a flash message only if responding with html?

如果用户启用了javascript,我不希望将这些消息放入flash中,因为在用户已经处理过javascript之后,它就会显示出来。是否有办法告诉Rails仅在用html响应时才显示flash消息?

1 个解决方案

#1


4  

You can put the flash bits of the code into your format.html block in order to achieve this.

您可以将这些代码的flash片段放入您的格式中。为了实现这一点,html块。

Maybe something like this:

也许是这样的:

@goal.save

respond_to do |format|
  format.html do
    if @goal.errors
      flash[:error] = array_to_list(@goal.errors.full_messages)
    else
      flash[:notice] = "Your goal was successfully added."
    end
    redirect_to :action => "show"
  end

  format.js
end

Have you thought about that you might want to have some kind of error message with the AJAX request as well? It might be a good idea if your AJAX request breaks by some reason so that stuff doesn't get saved.

您是否考虑过您可能也希望在AJAX请求中包含某种错误消息?如果AJAX请求由于某种原因而中断,那么这些东西不会被保存,这可能是个好主意。

#1


4  

You can put the flash bits of the code into your format.html block in order to achieve this.

您可以将这些代码的flash片段放入您的格式中。为了实现这一点,html块。

Maybe something like this:

也许是这样的:

@goal.save

respond_to do |format|
  format.html do
    if @goal.errors
      flash[:error] = array_to_list(@goal.errors.full_messages)
    else
      flash[:notice] = "Your goal was successfully added."
    end
    redirect_to :action => "show"
  end

  format.js
end

Have you thought about that you might want to have some kind of error message with the AJAX request as well? It might be a good idea if your AJAX request breaks by some reason so that stuff doesn't get saved.

您是否考虑过您可能也希望在AJAX请求中包含某种错误消息?如果AJAX请求由于某种原因而中断,那么这些东西不会被保存,这可能是个好主意。