如何制作死链接记者?

发布于 2024-11-04 18:14:51 字数 587 浏览 2 评论 0原文

我想在我的网站上创建一个按钮,用户可以单击该按钮来报告外部死链接。 我的项目表中的链接列中有该链接。

我想创建一个简单的电子邮件通知,其中包含项目 ID 和链接。 我正在考虑创建一个表单和某种可以处理该表单的控制器。

我的视图应该是这样的:

<% 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 %>

id 和 url 输入不应该只是像“报告损坏的链接”这样的链接可见。控制器应该获取这两个参数并向我发送电子邮件。

如何创建一个简单的死外部链接报告器?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荒人说梦 2024-11-11 18:14:51

您的问题可能更具体一些,但您可能想使用类似 Net:HTTP 的内容以及类似的内容:

  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

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
身边 2024-11-11 18:14:51

对于一个简单的断开链接报告器,我只会使用干代码的帮助程序,如下所示:

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

我建议您不需要使用表单,但如果您愿意,可以修改帮助程序。添加/删除您认为合适的参数,但项目 ID 可能足够简单,您可以在后端查找实际链接。只需在您的视图中使用它:

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

使用一些 ujs 以确保他们不会重新发布它:

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

希望这有帮助。

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.

痴情换悲伤 2024-11-11 18:14:51

看来您只是希望您的用户能够报告无效链接而不进行实际检查。你不需要一个表单,一个

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

应该就足够了,更不用说它看起来更好并且导致更少的样式问题等。在 Rails 3 中,这看起来会有所不同,我认为 :remote=>true 而不是 :method=>:post

您使用此 link_to 调用的控制器/操作应该构造并发送邮件(阅读有关 ActionMailer)。事实上,正如 M. Kohl 所建议的那样,该控制器实际上可以检查链路是否已失效。

请注意,您可以传递多个 :controller:action:id。您传递的所有内容都将在控制器/操作的 params 数组中可用。

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.

遮了一弯 2024-11-11 18:14:51

正如您所解释的,您只需要一个 发送邮件的邮件程序用户报告损坏的链接,没有什么异常。

其他选项包括创建一个 rake 任务来迭代链接并报告损坏的链接,其代码类似于 Michael Kohl。这不需要用户触发邮件程序:

# In lib/tasks/ (rake links:test)
require 'net/http'    
namespace :links do
  desc "Test for broken links in DB."
  task :test => :environment do |t, args|
    count = 0
    Entries.all.each do |e|
      count += 1
      # is_live? implementation is similar to Michael's
      puts "ERR: #{e.url} (Entry id: #{e.id})" unless is_live?(e.url)
    end
    puts "#{count} tested links."
  end
end

As you are explaining it, you just need a mailer that sends user reported broken links, nothing unusual.

Other options include creating a rake task which iterates over your links, and reports broken ones, with code similar to Michael Kohl. This doesn't require a user triggered mailer:

# In lib/tasks/ (rake links:test)
require 'net/http'    
namespace :links do
  desc "Test for broken links in DB."
  task :test => :environment do |t, args|
    count = 0
    Entries.all.each do |e|
      count += 1
      # is_live? implementation is similar to Michael's
      puts "ERR: #{e.url} (Entry id: #{e.id})" unless is_live?(e.url)
    end
    puts "#{count} tested links."
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文