问题
Rails 4
My application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
module Helpdesk
class Application < Rails::Application
config.rakismet.key = '3sf1b9e19da3'
config.rakismet.url = 'http://127.0.0.1:3000/'
end
end
My model:
class Ticket < ActiveRecord::Base
include Rakismet::Model
has_many :pictures, as: :imageable
belongs_to :status
belongs_to :stuff
belongs_to :department
validates :customer_name, :customer_email, :subject, :body, presence: true
def init_sp(permalink, request)
self.permalink = permalink
self.remote_ip = request.remote_ip
self.user_agent = request.env["HTTP_USER_AGENT"],
self.referrer = request.env["HTTP_REFERER"]
end
rakismet_attrs author: :customer_name, author_url: :permalink, author_email: :customer_email, content: :body,
permalink: :permalink, user_ip: :remote_ip, user_agent: :user_agent, referrer: :referrer
end
My controller:
class TicketsController < ApplicationController
def new
@ticket = Ticket.new
end
def create
@ticket = Ticket.new(ticket_params)
@ticket.init_sp(ticket_show_path(Ticket.generate_id), request)
t = Logger.new(STDOUT)
t.debug "================================"
t.debug @ticket
t.debug @ticket.spam?
t.debug @ticket.akismet_response
t.debug "================================"
if @ticket.save
flash[:notice] = "Ticket created successfully. Message sent."
redirect_to ticket_show_path(@ticket.token)
else
render "new"
end
end
My Log:
#<Ticket id: 5, status_id: 1, customer_name: "Drobazko", customer_email: "drobazko@gmail.com", token: "QOI-017-QIT-078-ULR", body: "Viagra", subject: "Subj", created_at: "2014-06-14 06:45:58"
, updated_at: "2014-06-14 06:45:58", stuff_id: nil, department_id: nil, permalink: "/tickets/XQT-689-KZR-289-USQ", remote_ip: "127.0.0.1", user_agent: ["Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36", "http://127.0.0.1:3000/tickets/new"], referrer: "http://127.0.0.1:3000/tickets/new">
D, [2014-06-14T09:45:59.484731 #7688] DEBUG -- : nil
D, [2014-06-14T09:45:59.485731 #7688] DEBUG -- : false
I.e. t.debug @ticket.spam? returns nil, t.debug @ticket.akismet_response returns false
Any ideas?
回答1:
Oh man: I tried to help some guy fix his code, it was not ment as an example of how to use rakismet. You should check the rakismet docs.
- Imho the whole
init_spthing is not needed. As mentioned in the other answer: if you call.spam?from the controller, it will have access to therequest. - I would not make author_url=permalink, it should be a an url entered the author (of the ticket), which could be an indication if the author is a spammer. You can just leave it blank.
- Why don't you use the
Rails.logger.debug? - Did you notice you just shared your akismet key with the world?
But aside of that, it is little weird that .spam? return nil, which from the source seems impossible. However a akismet_response could well be false. So afaik akismet just doesn't think it is spam.
From the rakismet documentation:
The only guaranteed way to trigger a positive spam response is to set the comment author to "viagra-test-123".
来源:https://stackoverflow.com/questions/24217458/rails-4-rakismet-akismet-not-working-properly