, . , , :
-:
class Website < ActiveRecord::Base
require 'net/http'
belongs_to :user
before_create :generate_unique_id
def verify!
verify_unique_id unless self.verified == true
end
protected
def generate_unique_id
self.unique_id = ActiveSupport::SecureRandom.hex(10)
end
def verify_unique_id
response = Net::HTTP.start(self.domain, 80) {|http| http.head("/# {unique_id}.html") }
self.verified = true if response.code == "200"
end
end
, , -:
def verify
@website = Website.find_by_id(params[:website])
@website.verify!
respond_to do |format|
if @website.save && @website.verified == true
flash[:notice] = 'Site verified!'
format.html { redirect_to(websites_path) }
format.xml { head :ok }
else
flash[:notice] = 'Site verification failed!'
format.html { redirect_to(websites_path) }
format.xml { render :status => :unprocessable_entity }
end
end
end
, ( , ):
<% for website in @websites %>
<%= link_to "#{website.domain}", website %> |
<% if website.verified? %> VERIFIED |
<% else %> NOT VERIFIED
<%= link_to "Verify your website", verify_website_path(:website => website.id), :id => website.id %>
Verification key: <%= website.unique_id %>
<% end %><% end %>
<%= link_to "Add a website", new_website_path %>
In any case, I conducted this through several manual tests with one of my existing websites, and it works without problems. I still, of course, have to perform other checks, but this was the one I really need help with. Thanks guys!
Kenji
source
share