code

플래시 메시지가 사라지지 않는 이유는 무엇입니까?

codestyles 2020. 10. 20. 07:40
반응형

플래시 메시지가 사라지지 않는 이유는 무엇입니까?


컨트롤러에서 예외 처리를 수행하고 있습니다. : create 작업에서 예외가 발생하면 : new 작업으로 렌더링하고 플래시 메시지를 표시합니다.

모든 것이 잘 작동하고 예외가 발생하면 플래시 메시지를 볼 수 있지만 다른 페이지로 리디렉션 (손으로 클릭) 하면 플래시 메시지가 여전히 여기에 있습니다 . 그런 다음 다른 페이지 ( 두 번째 클릭)로 리디렉션 하면 메시지가 사라질 수 있습니다.

이유가 무엇인지 아는 사람?

내 컨트롤러 코드 :

class MessagesController < ApplicationController
  rescue_from Exception, :with => :render_new

  def new
  end

  def create
  end

private
  def render_new
    flash[:alert] = t("uploading_error")
    render :action => :new
  end
end

내 레이아웃 코드 (Haml) :

%body
  #content
    - unless flash[:alert].blank?
      #alert= flash[:alert]

바꾸다

flash[:alert] = t("uploading_error")

flash.now.alert = t("uploading_error")

그리고 그것이 당신이 기대하는 결과인지 보십니까?

flash[:alert]다음 페이지를 위해 머물 것입니다 (따라서 두 번째 리디렉션에서만 사라집니다). 그러나 flash.now.alert현재 페이지에 대해서만 표시됩니다.


flash.now와 일반 플래시 사이에서 결정하는 것은 당장 고통스럽고 내 경험상 매우 취약합니다. 나는 일반 플래시를 사용하고 플래시를 표시하는 부분을 수정하여 사용자가 본 후 각 플래시의 내용을 삭제합니다. 나는 이것이 더 낫다고 생각한다.

a) 그것에 대해 생각할 필요가 없습니다

b) "사용자가 본 적이 있습니까?" (예 : "플래시가 부분적으로 렌더링 되었습니까?") 앱의 논리보다는 플래시를 지 울지 여부를 결정하는 가장 좋은 기준입니다.

내 플래시 부분은 다음과 같습니다. 또한 플래시를 강조 표시하기 위해 약간의 jquery를 사용합니다 (즉, 잠시 동안 노란색으로 깜박임).

<div id="flashes">

  <% if flash[:notice] %>
    <p id="flash_notice" class="messages notice"><%= flash[:notice] %></p>
    <%= javascript_tag "$('#flash_notice').effect('highlight',{},1000);" %>
  <% end %>

  <% if flash[:error] || flash[:errors] %>
    <p id="flash_errors" class="messages errors"><%= flash[:error] || flash[:errors] %></p>
    <%= javascript_tag "$('#flash_errors').effect('highlight',{},1000);" %>
  <% end %>

  <% flash[:error] = flash[:errors] = flash[:notice] = nil %>
</div>

대안은 다음과 같이 부분 끝에 flash.clear 를 사용하는 것입니다 .

<% if !flash.empty? %>
  <div class="flash-messages-container">
    <% flash.each do |name, msg| %>
      <% if msg.is_a?(String) && [:success, :info, :error, :warning].include?(name) %>
        <div class="flash-message" data-type="<%= name %>" >
          <%= msg %>
        </div>
      <% end %>
    <% end %>
  </div>
  <% flash.clear %>
<% end %>

이전에는 같은 문제가 있었지만 이제는 이것을 통해 해결 되었습니다. 코드에서 이것을 시도하십시오 .

<script type="text/javascript">
  $(document).ready(function(){
    setTimeout(function(){
    $('#flash').fadeOut();
    }, 2000);
  })
</script>

Even this does not work......certain types of exceptions like syntax errors...will prevent any type of cookie, flash or parameters from being transferred from controller to view. Your only option is to use a session key and then clear it after showing the error.

Try your solution with a syntax error...you should see that your message won't appear in the redirected page with anything else except with a session key.....


I also suggest to clear the flash inner hashes upon displaying. flash.clear will do the trick in a clean way :

      <% flash.each do |key, value| %>
       <div class="alert alert-<%= key %>">
        <%= value %>
       </div>
      <% end %>
      <% flash.clear %> #this line clears the object

http://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html#method-i-clear

참고URL : https://stackoverflow.com/questions/4613952/why-flash-message-wont-disappear

반응형