Reputation: 9184
In my model I have:
class ArticleComment < ActiveRecord::Base
include Rakismet::Model
validates :text, :presence => true
belongs_to :article
belongs_to :user
comment, permalink, request, username, email, text, remote_ip,
user_agent, referrer = nil
def init_sp(comment_, permalink_, request_, username_, email_, text_)
comment, permalink, request, username, email, text =
comment_, permalink_, request_, username, email_, text_
remote_ip = request_.remote_ip
user_agent = request_.env["HTTP_USER_AGENT"],
referrer = request_.env["HTTP_REFERER"]
end
rakismet_attrs author: username, author_url: permalink, author_email: email,
content: text, permalink: permalink, user_ip: remote_ip,
user_agent: user_agent, referrer: referrer
binding.pry
end
and in controller:
def create
@article_comment = ArticleComment.new(article_comment_params)
@spam = @article_comment.init_sp(@article_comment, params[:permalink],
request, username, email, article_comment_params[:text])
if !@article_comment.spam?
....
So I need to set up field's like ip
, user_agent
, text
in controller, how could I do this?
Now I see that my value's are nil ( why?
How to set rakismet_attrs value's with help of controller?
Upvotes: 0
Views: 1904
Reputation: 50057
Some small pointers:
self.column_name
So your model should look as follows:
class ArticleComment < ActiveRecord::Base
include Rakismet::Model
validates :text, :presence => true
belongs_to :article
belongs_to :user
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: user.name, author_url: user.permalink, author_email: user.email,
content: text, permalink: permalink, user_ip: remote_ip,
user_agent: user_agent, referrer: referrer
end
And in your controller, just write
def create
@article_comment = ArticleComment.new(article_comment_params)
@article_comment.init_sp(params[:permalink], request)
if !@article_comment.spam?
...
Also note, that according to the documentation you do not need to store request environment parameters, if you call .spam?
inside a controller method (as you already do) it can access the request
environment by itself.
Upvotes: 1
Reputation: 464
Small upgrade on @nathanvda answer, also assuming you already have permalink in article_comment_params:
in controller:
def create
@article_comment = ArticleComment.new(article_comment_params)
@article_comment.spam_check_data = spam_check_data_from_request
if !@article_comment.spam?
...
private
def spam_check_data_from_request
{
remote_ip: request.remote_id,
user_agent: request.env["HTTP_USER_AGENT"],
referrer: request.env["HTTP_REFERER"]
}
end
in model
class ArticleComment < ActiveRecord::Base
include Rakismet::Model
validates :text, :presence => true
belongs_to :article
belongs_to :user
def spam_check_data=(data)
self.remote_ip = data[:remote_id]
self.user_agent = data[:user_agent]
self.referrer = data[:referrer]
end
rakismet_attrs author: username, author_url: permalink, author_email: email,
content: text, permalink: permalink, user_ip: remote_ip,
user_agent: user_agent, referrer: referrer
end
this allows you to decouple spam check data from the request object.
Upvotes: 0
Reputation: 5802
Now I see that my value's are nil ( why?
When you call article_comment_params
the values are nil because Rails 4 uses strong parameters by default, so the parameters are filtered out because you do not explicitly permit
them.
In the method article_comment_params
you need to explicitly permit every parameter you want to whitelist / allow through the controller.
Change your article_comment_params
method to include the permit
method:
def article_comment_params
params.require(:article_comment).permit(:comment, :permalink, :request, :username, :email, :text)
end
I don't know exactly what params you want to allow, so make sure you go through the list and include every parameter you need.
How to set rakismet_attrs value's with help of controller?
The difficulty with this issue should be resolved when you whitelist the parameters using the method described above.
Upvotes: 0