I am having a javascript code to compare two strings and execute code accordingly.

One String retrieved though rails flash[:alert] and other specified locally.

     <% if flash[:message].present? %>
         var message=new String("<%= flash[:message] %>");
         var message_status=message.localeCompare("mail_sent");
         if(message_status==0)
         {
           // if code....
         }
         else
         {
           alert(message);
           // else code....
         }
    <% end %>

Here, ultimately, i am trying to compare two Strings. One retrieved through flash[:alert] and other specified locally i.e mail_sent.

What I observe is

It always goes through the else part even though the message displayed in the else part alert is mail_sent.

Please Note that when i replaced the <%= flash[:message] %> with String locally. It executed the if part. i.e

var message=new String("mail_sent");

instead of

var message=new String("<%= flash[:message] %>");

Could anyone say why is it so ?

What is need to be done in order to compare two strings in this way?

有帮助吗?

解决方案

please check your controller. There might be something wrong there. Are you sure you are setting flash[:message] ="mail_sent" ????

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top