2012-04-10 10 views
1

私のアプリケーションでフラッシュメッセージに問題があります。実際に私のアプリケーションでは、私はユーザー認証とRuby 1.9.3とレール3.2.2で私のアプリケーションのための工夫を使用しています。ruby​​ 1.9.3とレールのフラッシュメッセージに関する問題3.2.2

ユーザーがログインしてログアウトし、新しいアカウントを登録すると、devise flash [:notice]は正常に動作しています。

In Railsのフラッシュ[:notice]とflash [:alert]は、デフォルトのフラッシュメッセージです。

フラッシュメッセージは、ユーザーが工夫フラッシュログインする場合、問題がある、他のページに一度だけときに、ページのリロード時や、ユーザーが負の1ページから

表示されている[:告知]私はリロードしたときに表示されるが、 [:notice]が表示されていますが、レールでは[:notice]は1回だけ表示されます

実際には、新しい投稿を作成しようとしたときに表示ページにリダイレクトして書き込みますフラッシュメッセージのヘルパーメソッドこのメソッドは、私はフラッシュメッセージを表示するためのアプリケーションのレイアウトから呼び出している。コントローラで

メソッド

def create 
    @asset = Asset.new(params[:asset]) 
    @asset.user_id = current_user.id 

    respond_to do |format| 
    if @asset.save 
     format.html { redirect_to @asset, alert: 'Asset was successfully created.' } 
     format.json { render json: @asset, status: :created, location: @asset } 
    else 
     format.html { render action: "new" } 
     format.json { render json: @asset.errors, status: :unprocessable_entity } 
    end 
    end  
end 

フラッシュメッセージを

FLASH_TYPES = [:error, :warning, :success, :message,:notice,:alert] 

def display_flash(type = nil) 
    html = "" 
    if type.nil? 
    FLASH_TYPES.each { |name| html << display_flash(name) } 
    else 
    return flash[type].blank? ? "" : "<div class=\"#{type}\"><p>#{flash[type]}</p>  </div>" 
    end 
    html.html_safe 
end 

を表示するためのヘルパーメソッドを作成し、私は私が試みたアプリケーションレイアウト

= display_flash 

を形成し、このメソッドを呼び出しましたフラッシュ[:アラート]、フラッシュ[:エラー]、フラッシュ[:メッセージ] [:予告]

液にこの問題を助けてください

答えて

-1

のHy私が作るmessage.Firstフラッシュを表示するには、このアプローチを使用して使用していますページと私は、これはまた、唯一の フラッシュ表示宝石と呼ばれるflash_messageで試してみましたこの部分

<% [:alert, :notice, :error].select { |type| !flash[type].blank? }.each do |type| %> 
<p> 
    <% if flash[:notice] %> 
    <div class="alert-message error"> 
     <h2 style="color: #ffffff;">Notice:</h2> <br/> 
     <%= flash[type] %> 
    </div> 
<% elsif flash[:error] %> 
    <div class="alert-message error"> 
     <h2 style="color: #ffffff;">Errors</h2> <br/> 
     <% flash[:error].each_with_index do |error, index| %> 
      <%= index+1 %>. <%= error %> <br/> 
     <% end %> 
    </div> 
    <% end %> 


    </p> 
    <% end %> 

のshared.Theコードで部分的

_flash.html.erbと私はこの

のようなアプリケーションのレイアウトでそれを呼び出します
<div id="flash"> 
    <%= render :partial => 'shared/flash', :object => flash %> 
    </div> 

とコントローラの使用通知では、この

flash[:notice] = 'Admin was successfully created.' 
    flash[:alert] = 'Admin was successfully created.' 

しかし、それはこの

 def create 
     @user = User.new(params[:user]) 
     @user.is_activated = true 
# @user.skip_confirmation! 
if @user.save 
    role = Role.find_by_name("admin") 
    RoleUser.create!(:user => @user, :role => role) 
    redirect_to :controller => '/administrator', :action => 'new' 
    flash[:notice] = 'Admin was successfully created.' 
else 
    flash[:error]=[] 
    @user.errors.full_messages.each do |error| 
    flash[:error] << error 
    end 

    render :action => "new" 
end 

エンド

one.Likeよりも可能性があるので、私は、配列を使用して、エラーを示すように警告この行をapplication.jsに追加してください。

setTimeout("$('#flash').html(' ');", 10000); 

それを使って楽しんでください!!!!!!!!!!!!!!

関連する問題