title: Displaying flash messages on modal dialog box on submit rails
body: <p>I am trying to build the modal box for registering emails addresses in rails.
  I have done it but can not able to find the way to display the flash messages on modal dialog box when registration fails.
  On submit the box gets closed and message is only getting display on home page. I do not have good knowledge of web or rails, but would be nice if I can get some pointers. </p>
  
  <p>The functionality is working fine expect on fail, dialog box should display the messages on it instead of on home page. I know where is issue in my code but do not know approach to do it. Here is my code for dialog box</p>
  
  <pre><code>   &lt;div id="register" class="modal fade" role="dialog"&gt;
        &lt;div class="modal-dialog"&gt;
          &lt;!-- Modal content--&gt;
          &lt;%= form_for(@email, url: home_esave_path, :html =&gt; {class:"model-dialog"}) do |f| %&gt;
          &lt;div class="modal-content"&gt;
            &lt;div class="modal-header"&gt;
              &lt;button type="button" class="close" data-dismiss="modal"&gt;&amp;times;&lt;/button&gt;
              &lt;h4 class="modal-title"&gt;Register&lt;/h4&gt;
            &lt;/div&gt;
            &lt;div class="modal-body"&gt;
              &lt;div class="form-group"&gt;
                &lt;%= f.text_field :email, class:"form-control",  placeholder: "Enter a valid gmail address" %&gt;
              &lt;/div&gt;
              &lt;%= f.show_simple_captcha(:email=&gt;"email", :label =&gt; "Human Authentication", :refresh_button_text =&gt; "Refresh text", class:"form-control", :placeholder =&gt; "Enter the code") %&gt;
              &lt;%= f.submit "Register me!",  :class =&gt; "btn register_button" %&gt;
            &lt;/div&gt;
            &lt;div class="modal-footer" style="text-align:left"&gt;
              &lt;!-- show in case of success --&gt;
              &lt;% if @email.errors.any? %&gt;
              &lt;div id="error_explanation"&gt;
                &lt;h2&gt;
                  &lt;%= pluralize(@email.errors.count, "error") %&gt; prohibited
                  this email from being saved:
                &lt;/h2&gt;
                &lt;ul&gt;
                  &lt;% @email.errors.full_messages.each do |msg| %&gt;
                  &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
                  &lt;% end %&gt;
                &lt;/ul&gt;
              &lt;/div&gt;
              &lt;% end %&gt;
              &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;% end %&gt;
        &lt;/div&gt;
        &lt;/div&gt;
  </code></pre>
  
  <p>my controller code to get the messages</p>
  
  <pre><code>def save
  @email = Email.new(user_params)
  if !user_params.nil?
    if @email.save_with_captcha
      flash[:success] = "Success! The email address #{@email.email} has been successfully registered. We will contact you as soon as we have available rooms for new beta testers."
      redirect_to :action =&gt; 'index'
    else
      if @email.valid?
        flash[:error] = "The captcha is not correct, enter again the captcha."
        redirect_to :action =&gt; 'index'
      else
        flash[:error] = "You have put invalid #{@email.email}"
        redirect_to :action =&gt; 'index'
      end
    end
  end
  </code></pre>
  
  <p>end
   Flash messages I am displaying outside this model which is obvious that will be displayed on home page. But I did not find way to display it on model dialog box. </p>
  
  <p>here is how I am displaying the messages</p>
  
  <pre><code>  &lt;% flash.each do |type, message| %&gt;
        &lt;div class="alert &lt;%= alert_for(type) %&gt; fade in"&gt;
          &lt;button class="close" data-dismiss="alert"&gt;×&lt;/button&gt;
           &lt;span class="sr-only"&gt;Close&lt;/span&gt;
          &lt;%= message %&gt;
        &lt;/div&gt;
        &lt;% end %&gt;
  </code></pre>
  
  <p>Thanks for suggestions</p>