Tag Archives: links

Forcing a Link to Behave Normally – AngularJS

Hey everyone,

Another quick post. Working with AngularJS this morning I had a need for a link to redirect to a login page that was not contained within the “angular” part of the app. Unfortunately Angular was overriding the behavior and trying to route it.

The solution is fairly easy for this, simply add a target attribute:

 
<!-- Original -->
<a href='/account/login' alt='Sign In'>Sign In</a>
 
<!-- Solution -->
<a href='/account/login' alt='Sign In' target="_self">Sign In</a>

There are a few other solutions depending on your use case, check out the following StackOverflow post for more info: http://stackoverflow.com/a/16838013/522859

Stop a Link from Going Anywhere – CSS/JavaScript

Hey everyone,

Another quick one – how to stop a link from doing anything. I’ve just come across a need to use a link that acts as a button. A slight UI issue with this is that clicking a hyperlink will with an href value of ‘#’ will push scroll to the top of the page.

There are a couple of ways to go about fixing this. Note the href value in the following:

1
<a href='javascript:void(0)' class='btn_show_reply' data-comment_id="<%= comment.id %>">Reply</a>

The second option is probably already familiar to anyone using ajax forms, simply have your function return false:

1
2
3
4
5
6
7
8
9
10
11
12
<a href='javascript:void(0)' onClick='myFunction()' id='my_awesome_link' data-comment_id="<%= comment.id %>">Reply</a>
 
<script type='text/javascript'>
function myFunction(){
 
   //Do some awesome stuff
   // ...
 
   //Return false to prevent link from affecting anything
   return false;
}
</script>

The third option is simply a jQuery-ish rehash of number 2:

1
2
3
4
5
6
7
8
<script type='text/javascript'>
 
/* Does some awesome stuff while preventing the calling link from directing the user */
$('#my_awesome_link').click(function(e) {
 
   //Do some awesome stuff and then prevent the link from directing user
   e.preventDefault();
});

Allow HTML String – Ruby on Rails

Just a quick post on how to prevent escaping for HTML within a string. Simply use html_safe?.

i.e. The following will show the bold tags as text:

<%= "<b>GOOOOOOOGLE</b>">

<b>GOOOOOOOGLE</b>

Whereas once we add html_safe the text will be displayed in bold:

<%= ("<b>GOOOOOOOGLE</b>").html_safe?>

GOOOOOOOGLE

undefined method `merge’ for 5:Fixnum – Hidden Field

Just a quick post on an error I ran into today while using hidden fields:

undefined method `merge' for 5:Fixnum
 
27:   <div class="actions">
28:     <%= f.submit :class => 'button' %>
29:   </div>
30:   <%= f.hidden_field(:product_id, @upload.product_id) %>
31: <% end %>
32:

Thankfully this is an easy fix, simply amend your code as follows:

1
2
3
4
5
<div class="actions">
    <%= f.submit :class => 'button' %>
</div>
   <%= f.hidden_field :product_id, :value => @upload.product_id %>
<% end %>

And that’s all there is to it, let me know if there are any issues.

Submit form_for to Custom Action – Ruby on Rails

Just another quick problem I ran into with rails this afternoon – how to submit a form_for to a custom action. Luckily there’s quite a bit on how to do this in the documentation, my solution ended up as follows:

<%= form_for @feedback, :url => url_for(:controller => 'feedbacks', :action => 'leave_seller_feedback') do |f| %>

Just substitute your form_for tag with the one above and put in the relevant details and it should all work fine. Keep in mind that the submit tag doesn’t need to be changed, just the form_for tag.

Flash Notice Appearing Twice – Ruby on Rails

Ran into another flash notice issue, my messages were not disappearing after the first view. My code was as follows:

#Create flash warning
flash[:warning] = ("This product has been replaced: <a href='#{url_for(new_product)}'>" +new_product.title + "</a>").html_safe

It turns out that all you have to do to fix this issue is the following:

flash.now[:warning] = ("This product has been replaced: <a href='#{url_for(new_product)}'>" +new_product.title + "</a>").html_safe

This blog gives a brief overview of when to use flash.now[notice] and when to use flash[:notice]. A basic rule of thumb is that if you’re using a redirect, use flash[:notice], if you’re simply rendering a view, use flash.now[:notice].

link.smartscreen.live.com – Google Analytics Referral

Well, today I was looking through the Google Analytics overview of the WhatIBroke.com and I noticed a referral I hadn’t seen before from link.smartscreen.live.com.

I did a bit of Googling and it turns out that this is actually something Microsoft uses within IE and MSN. Apparently, whenever someone clicks on a link in msn the service will check the link against a list of unsafe sites and warn the user if anything is amiss.

It just so happens that I’d sent a link to the blog to a friend on msn – so I guess everything checks out. Here’s a bit of a blurb from the FAQ on Microsoft’s Site:

SmartScreen Filter is a feature in Internet Explorer that helps detect phishing websites. SmartScreen Filter can also help protect you from downloading or installing malware (malicious software).

SmartScreen Filter helps to protect you in three ways:

As you browse the web, it analyses webpages and determines if they have any characteristics that might be suspicious. If it finds suspicious webpages, SmartScreen will display a message giving you an opportunity to provide feedback and advising you to proceed with caution.

SmartScreen Filter checks the sites you visit against a dynamic list of reported phishing sites and malicious software sites. If it finds a match, SmartScreen Filter will show you a warning notifying you that the site has been blocked for your safety.

SmartScreen Filter checks files that you download from the web against a list of reported malicious software sites and programs known to be unsafe. If it finds a match, SmartScreen Filter will warn you that the download has been blocked for your safety. SmartScreen Filter also checks the files that you download against a list of files that are well known and downloaded by many Internet Explorer users. If the file that you’re downloading isn’t on that list, SmartScreen Filter will warn you.

 
Anyway, that’s all I’ve got for now – Good Luck!