Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

undefined method `each' for "":String (mongoid/middleware.rb:13:in `is_unique?' ruby1.9.2) #26

Closed
fwoeck opened this Issue · 11 comments

2 participants

@fwoeck

Hi Brian!

When I fire a
http://127.0.0.1:3000/validators/uniqueness.json?case_sensitive=true&scope=&user[email]=user10%40mail.com

I get an
undefined method `each' for "":String

error here:
lib/client_side_validations/mongoid/middleware.rb:13:in `is_unique?'

That's this line:
(params[:scope] || {}).each do |key, value|

Could it be, that in this case params[:scope] is not a Hash but a string and ruby1.9.2 bails out?
--Frank

@bcardarella
Owner

Hi Frank,

Does the validation in the model have a scope?

What I'm seeing is the scope param has no value in the URL you included. Client Side Validations should set a default value for the scope for an existing record, but if the scope is for an attribute that is on the current form the value of that attribute on the form will override the default value.

I develop on 1.9.2 so I don't think that's a factor in this case.

If you could paste in the model as well as the form that would be a great help. Thanks! :)

@bcardarella
Owner

I'm guessing that this is a new record and the value for that scope can not be derived from the form. (either it hasn't been entered in or it is set on the server when creating the record)

@bcardarella
Owner

If this is the case there is a strategy to have this work:

On the controller set the attribute on the model to be validated that will be scoped against. This will set the default scope value the validations will use.

In any case, this a legit bug. If the scope param is empty, and there are cases that it can be, it should just fall back to a server side validation instead of crashing. I'll include as a bug for 3.0.1. Thanks! :)

@fwoeck

Thank you for your quick reply! I'm not sure if I understand "scope" fully. Actually the controller action to render the form stems from devise, it's a session controller:

def new
clean_up_passwords(build_resource)
render_with_scope :new
end

And the form looks like this:

= form_for(resource, :as => resource_name, :url => session_path(resource_name), :validate => true) do |f|
= f.label :email
= f.email_field :email

Im terms of mongoid "scopes" there's no default scope defined - but this is not what you mean, right?
--Frank

@bcardarella
Owner

Frank,

Is this a default devise generated model?

@fwoeck

basically yes, with lot of added stuff. It looks like this:

validates_presence_of :email

attr_accessible :email, :password, :password_confirmation,
:name, :color, :avatar

devise :database_authenticatable,
:registerable,
:recoverable,
:rememberable,
:trackable,
:validatable,
:token_authenticatable,
:confirmable,
:lockable,
:timeoutable

@bcardarella
Owner

Ok, thanks very much. I'll start digging into it.

@fwoeck

Ok, thank you for your work!

@bcardarella
Owner

Fixed in: 7faa2f6

@bcardarella bcardarella closed this
@fwoeck

cool, thank you!

@bcardarella
Owner

3.0.1 is pushed so it should be all good. Let me know if anything else comes up! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.