Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a dead link reporter?

I want to make a button on my website that a user can click on to report at external dead link. The link do I have in my link column that is in my item table.

I want to create an simple email notice that have the ID of the item and the link. I was thinking creating an form and some sort of controller that could handle the form.

My view should look something like this:

<% for items in @items %>
        <%= simple_form_for @items] do |f| %>
        <%= f.hidden field :id, :value => 'item.id' %>
        <%= f.hidden field :url, :value => 'item.link %>
        <%= f.button :submit, :value => 'report broken link' %>
    <% end %>
 <% end %>

The id and the url inputs should not be viewable just a link like "report broken link". A controller should take the two params and send me an email.

How do I create a simple dead external links reporter?

like image 880
Rails beginner Avatar asked May 02 '11 14:05

Rails beginner


People also ask

How do I scan a broken link on my website?

Google Analytics. Google Analytics is a great free tool for tracking website performance, and it's also helpful for easily finding broken links. First, log into your Google Analytics account and click on the Behavior tab. Then select “Site Content” and then “All Pages.”

How do I find a dead link?

The Wayback Machine Chrome extension detects dead web pages and gives you the option to view an archived version of the page. Imagine it in action. A website with a 404 error or a Page Not Found message can be an annoyance. A slightly dated but still relevant version of the webpage is the next best option.


2 Answers

Your question could be a bit more specific, but you probably want to use something like Net:HTTP and something similar to this:

  uri = URI.parse(url)
  response = nil

  begin
    Net::HTTP.start(uri.host, uri.port) do |http|
      response = http.head(uri.path.size > 0 ? uri.path : "/")
    end
  rescue => e 
    ...
  end

  # handle redirects if you need to
  if response.is_a?(Net::HTTPRedirection)
   ...
  end

  if response.code == '404'
    ...
  end
like image 111
Michael Kohl Avatar answered Oct 06 '22 21:10

Michael Kohl


For a simple broken link reporter I would just utilize a helper for dry code, like the following :

module ApplicationHelper
  def report_broken_link_for( id )
    link_to "report broken link", {:controller => "reporting", :action => "report_broken_link", :id => id}, :class => "broken_link_reporter_link", :remote => true
  end

I suggest that you wouldn't need to use a form, but if you feel so inclined you can modify the helper. Add/remove parameters as you see fit, but the item id would probably be simple enough, you can lookup the actual link in the back end. Simply use it in your views :

<% @items.each do |item| %> 
  <%= link_to item.url %>
  <%= report_broken_link_for item.id %><br/>
<% end %>

Use some ujs to make sure they don't repost it :

$('.broken_link_reporter_link')
   .live('ajax:success', function(evt, data, status, xhr){
      $(this).replaceWith("thanks!");
 });

Hope this helps.

like image 42
bencode Avatar answered Oct 06 '22 21:10

bencode