How to make a dead link reporter?

It seems that you simply want your users to be able to report dead links without doing the actual check. You do not need a form for that, a

link_to "text", :controller=>ctrl, :action=>actname, :id=>item_id, :method=>:post

should be sufficient, not to mention that it would look nicer and cause less problems with styling, etc. In Rails 3 this would look different, I think :remote=>true instead of :method=>:post.

The controller/action you call with this link_to should construct and send mail (read about ActionMailer). In fact, this controller can actually check whether the link is dead or not, as M. Kohl suggested.

Note that you can pass more than :controller, :action and :id. All of what you pass will be available in the params array in the controller/action.


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.


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