Domanda

I have a partial with the functionality of "tell a friend about something". And I want to let the user know if everything went ok so I return in my controller to show the flashes.

The problem is I can see the flashes via pry (debugger) but on screen nothing is shown.

As shown below:

Pry Results: Pry results

**IMPORTANT:**Tell a friend is a page that is opened when clicking on a link. The link opens an index page which opens a partial.

I've tried different things:

  • return render :index
  • render :index
  • redirect
  • ...

The index.html.erb file:

<!doctype html>
<html lang="nl">
  <head>
    <meta charset="utf-8" />
    <title><%= t("share.index.header") %></title>
    <!--[if lt IE 9]><script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
    <%= javascript_include_tag "application" %>
    <%= stylesheet_link_tag "application", :media => "all" %>
    <style>
      body {
        margin: 2em;
        font: 14px/1em 'open-sans', sans-serif;
        color: #333;
        background: #eee;
      }
      h1 {
        font-weight: normal;
        color: #666;
      }
      p {
        color: #666;
        font-size: 12px;
        margin: 0 0 3em 0;
      }
      p a {
        color: #333;
        text-decoration: none;
        border-bottom: 1px dotted #666;
      }
      form {
        position: relative;
      }
      .col-2 {
        position: absolute;
        top: 0; left: 25em;
      }
      label {
        display: block;
        margin: 0 0 2em 0;
      }
      strong {
        font-weight: normal;
        display: block;
        margin: 0 0 .5em 0;
      }
      input[type=text] {
        padding: 6px;
        width: 25em;
        border: 1px solid #ccc;
        background: #fff;
      }
      input[type=submit] {
        color: white;
        font-size: 14px;
        padding: .75em;
      }
    </style>
  </head>
  <body>
    <div id="wrap">
      <% if current_user %>
        <%= render :partial => "signed_in_user" %>
      <% else %>
        <%= render :partial => "anonymous_user" %>
      <% end %>
    </div>
  <script>
  </script>
  </body>
</html>

The signed_in_user partial:

  <script type="text/javascript"charset="UTF-8">
    $(document).ready(function() {
        $( "#submitBtn" ).click(function() {
          // Simple input Validation
          var emailaddress =  $( "#email").val();
          if( !validateEmail(emailaddress)) { 
            $( "#validationerror").show();
            return false;
           };
        });

        function validateEmail($email) {
          var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
          if ($email == '' ){
            return false;
          } else {
            if( !emailReg.test( $email ) ) {
              return false;
            } else {
              return true;
            }
           };
       };
      });
  </script>
<!-- Why is this code not showing after return render -->
<!-- Why is this code not showing after return render -->
<!-- Why is this code not showing after return render -->
<p><%= flash[:notice]  %></p>
<!-- Why is this code not showing after return render -->
<!-- Why is this code not showing after return render -->
<!-- Why is this code not showing after return render -->
<h1><%= t :header, :scope => [:share, :index] %></h1>
<p><%= t :link_to_page_html, :scope => [:share, :index], :link => link_to(truncate(job_url(params[:job_id]),length: 50, omission: '...'), job_path(params[:job_id])) %></p>
<%= form_tag(job_share_index_path(params[:job_id]), :method => "post", :id => "myform", :remote => true) do %>
  <%= hidden_field_tag "guid", params[:job_id] %>
  <div class="col-1">
    <label>
      <strong><%= t("share.index.name") %></strong>
      <%= text_field_tag "fullname", "", :autofocus => "autofocus" %>
    </label>
    <label>
      <strong><%= t("share.index.email") %></strong>
      <%= content_tag(:p,  t("share.index.valid_email") , :id => "validationerror", :class => 'small_label' , :style => "display: none;color: red; font-weight: bold;") %>
      <%= text_field_tag "email", "", :placeholder => t("share.index.email_placeholder"), :id => "email" %>
    </label>
  </div> <!-- /.col-1 -->
  <div class="col-2">
    <label>
      <strong><%= t("share.index.message") %></strong>
      <%= text_area_tag "message", "", :style => "width: 370px; height: 175px;"%>
    </label>
  </div> <!-- /.col-1 -->
  <%= submit_tag t("share.index.button"), :class => "medium button", :id => "submitBtn" %>
<% end %>

The share controller

class ShareController < ApplicationController

  def index
    render :layout => false
  end

  def create
    Sap.tell_a_friend(params[:fullname], params[:message], params[:guid], params[:email], api_params_for_user)

    # respond_to do |format|
    #   format.html { redirect_to(job_share_index_path) }
    #   format.js
    # end

    flash[:notice] ="test"
    return render :index
  end

end
È stato utile?

Soluzione 3

So for starters I want to thank Zendist(Andreas Bjørn) and Jakob S.

I was thinking since it's only tell a friend function the only real validation that needs to happen is check email.

So instead of returning a flash message via rails I just return a message to the user via javascript if his email address is valid.

As shown here:

 $( "#submitBtn" ).click(function() {
          // Simple input Validation
          var emailaddress =  $( "#email").val();
          if( !validateEmail(emailaddress)) { 
            $( "#validationerror").show();
            return false;
           } else {
                $( "#validationnotice").text("hello world");
           };
        });

Thanks again for your input and help #greatcommunity!

Altri suggerimenti

flash is intended for messages that should be visible on the next action/page view/request. You are attempting to render the flash in the same action as you set it, that's not going to work.

You need to use flash.now to set the flash messages:

This method enables you to use the flash as a central messaging system in your app. When you need to pass an object to the next action, you use the standard flash assign ([]=). When you need to pass an object to the current action, you use now, and your object will vanish when the current action is done.

ie:

flash.now[:notice] ="test" # or flash.now.notice = "test"

Fetch your Flash like this: <%= content_tag :div, flash[:notice] %>

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top