Issue with flash messages in ruby 1.9.3 and rails 3.2.2

后端 未结 1 1254
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 04:35

I have a issue with flash message in my application. Actually in my application i have used the devise for users authentication and my application with ruby 1.9.3 and rails

相关标签:
1条回答
  • 2020-12-20 05:03

    Hy i am using using this approach to show flash message.First i make partial

    _flash.html.erb in shared.The code of this partial

     <% [: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 %>
    

    and i call it in application layout like this

      <div id="flash">
        <%= render :partial => 'shared/flash', :object => flash %>
      </div>
    

    And in controller use notice,alert like this

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

    But for showing errors i use array because it may be more than one.Like this

           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
    

    end

    add this line in application.js

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

    Use it and enjoy!!!!!!!!!!!!!!

    0 讨论(0)
提交回复
热议问题