Validating a blank field Rails Model - ruby

This may be somewhat basic but cannot find a definitive answer anywhere. I have set up a contact form within my app and have put in a hidden field that when completed disables the submit button with some Jquery. My attempt at stopping automated spam..
Can I also add some validations in my model?
validates :ghost, :presence => false
Looking at the docs this is invalid? I want the form to fail if this field is filled in. Not sure how to go about this one
EDIT
So I have now read that I could possibly use
validates_exclusion_of :ghost, :on => :create
Though this is still failing as i dont think i am passing the correct arguments.

:presence => false means that you disable presence validator.
You need to write own absence validation (though in Rails 4.0 such validation exists, absence: true).
validate :ghost_is_absent
def ghost_is_absent
errors.add :ghost if ghost.present?
end

I am sorry to say , but why are you trying to do things so differently, doing it this way will make things more confusing for any future developer working on this piece of validation.
First thing:
1) You can do the reverse of it , mark it as spam when the field is empty and vice versa and then simply check with the validation validates_presence_of :ghost
2)or if you want to protect spam use capcha (recapcha gem for that)
3) or if you want it do it your way only , then just add a custom validation
Try creating a custom validation.
validate :check_for_spam
def check_for_spam
errors.add_to_base "ghost is present this is a spam" if ghost.present?
end

If you want to check if :ghost is blank:
validates :ghost, inclusion: {in: ['']}
If you want to check if :ghost is nil, you have to rely to a custom validator.

Related

Rails Active Admin unpermitted parameter

I have some problem/issues with active admin on rails, specifically unpermitted params error:
existing active admin parameter
here is the existing active admin parameter
model associated with the main model im working with
As per active admin documentation I should be doin right, as the other attributes for dispatch_information model is being accepted by rails and I was able to read and write with out any issues. Just with this recently added attribute "custom_attorney". Associations already set. and with out declaring an attr_accessor on model file it says this error
No method error
as it seems it cannot read or detect the column that I added for dispatch_information model, while in my console its already there.
When I add it with attr_accessor "while it should not, just to proceed on the form page" then I fill in the attributes need, im getting weird stuff in my console
Console view
as you can see it seems it being added inside efile_order hash instead of dispatch_information_attribute hash, and at the bottom part of the image you can see it says unpermitted parameters, even I added it inside the correct attribute block, we can also notice that the other attributes pf dispatch_information works really fine, just this recently added custom_attorney attribute. I already did everything like migration and other stuff.
Form Input
here is my form where we can see that input is on the same block where dispatch_defendant and dispatch_plaintiff is included and those two attribute works fine as well.
I really dont know what I missed here. TIA
The problem is that custom_attorney should be nested under dispatch_information_attributes you have it in the wrong place so it's unpermitted.
The correct way to do that is to add a block for those attributes and nest them.
- f.simple_fields_for :dispatch_information do |d|
- d.input :custom_attorney, :input_html => { id: 'new-attorney' }
It may be a good idea to provide an object for dispatch_information if you care for existing data. Assuming your ivar is named #e_filling_order then you should have the following.
- f.simple_fields_for :dispatch_information, #e_filling_order.dispatch_information || #e_filling_order.build_dispatch_information do |d|

Rails - How to use the 'has_secure_password' technique for emails

Rails 3.1.0 - Ruby 1.8.6
I'm trying to create a signup page. I came across this 'has_secure_password' with basically does all the magic for you to enter your password twice and checks if they are correct.
I'd like to use this technique to check on email address as well. If it's not possible, what is the easiest way for me to get the user to key in their email twice and check if they match?
Please help. I'm a rails noob.
Thanks
Rails has a validates_confirmation_of validation built in. If your model has
class User < ActiveRecord::Base
attr_accessor :email_confirmation
validates_confirmation_of :email
end
Then you just need an email confirmation text field in your form and rails will check that they match.

Case insensitive uniqueness validation in a Ruby Sequel.migration

I'm trying to figure out a good validation to use in my migration that will require case-insensitive uniqueness for user email addresses. In short, I want something like validate :email, :uniqueness => {:case_sensitive => false} without having to convert everything to use Rails or ActiveRecord. I could run emails through regexes but I don't like that solution.
I found a comment[1] saying you could use
validates_unique(:email){ |ds| ds.opts[:where].args.map! { |x| Sequel.function(:lower, x)}; ds}
but I don't understand what that code is doing and I don't want to use that code when I have no idea what that ds object is or what all is going on (why map!, does postgresql have a Sequel.function of :lower? ... probably, but I just don't know.)
[1] http://comments.gmane.org/gmane.comp.lang.ruby.sequel/6447
So I need one of two things answered:
1) How do I perform a case-insensitive uniqueness validation in a pure Sequel.migration (no ActiveRecord, no Rails)?
- OR -
2) If that code snippet I found online is actually what I want, what does it do & how does it work? (What is the ds object and what does this validation do with my database?)
As the Tin Man mentioned, you are confusing validations and constraints. You say you are trying to add a constraint and talk about Sequel.migration, but those have nothing to do with validations.
If you want to add a database constraint, you need to do something like this in a migration:
alter_table(:table){add_unique_constraint Sequel.function(:lower, :email)}
This is done so that the database doesn't allow duplicate emails in a case insensitive manner.
Validations are just for presenting nice error messages to the user. They are run before saving so that instead of the database raising a exception (which is difficult to deal with), you get a nice error message.
Like that comment mentions, you can't use validates_unique for case insensitive lookups on case sensitive databases without a hack. It would require that validates_unique accept an additional option (which may be added in the future).
If you don't want to use a hack like that, you'll have to do the validation manually:
dataset = model.where{|o| {o.lower(:email)=>o.lower(email)}}
dataset.exclude(pk_hash) unless new?
errors.add(:email, 'is already taken') unless ds.count == 0
In terms of what that hack does, ds is a Sequel::Dataset instance that validates_unique uses to check for uniqueness. If you do validates_unique :email, it'll be something like:
model.where(:email=>email)
# WHERE email = 'some email'
ds.opts[:where] extracts the where clause from that dataset, and transforms the arguments, wrapping them in SQL lower function calls, in order to transform the where clause so that it is similar to:
model.where{|o| {o.lower(:email)=>o.lower(email)}}
# WHERE lower(email) = lower('some email')
It's a hack as it only works if the model's dataset is not already filtered.

Rails - how to update model without required items

I have a model that contains required and free fields. Between the required items is also item with password.
In the _form.html.erb I hid the input with a password, so when the user want to update the form, so he will be update the all items except password => that's what I want. But because the item password is require, so I will get an error about bad validations.
I would like to ask you, if exist a good way to do this... I can't find some trick for it
I think it's :
validates_presence_of :password, :on => :create

Rails and mongoid: Validation is called on the referenced document upon saving/updating the referrer document?

I have a model Tracker that references_many Users.
Everything works fine, but for some business reasons, sometimes my
Users model are in a situation were often they fails the validation
rules (on purpose).
The problem is that I still need to update my Trackers in parallel of
that. And to my surprise saving the tracker will trigger the
validation rules for my User model as well... and obviously then the
saving of the tracker fails.
I could save my tracker with save :validate => false, but I don't want
to do that I have specific validation rules on the Tracker itself that
I want to be respected.
I also tried to play with deactivating callbacks but could not get it
to work...
Help please !
Alex
Ok, finally figured it out:
references_many :referees, :class_name => "User", :validate => false
This will do the trick !

Resources