Rails 3.2.19, Ruby 1.9.3p547.
After adding a few gems to development (pry and its various dependencies), any validation in the form validates :some_field, some_default_rails_validator: true fails. Prior to bundling, all of these validations worked perfectly. Rolling back the Gemfile and re-bundling has no effect.
Other folks seem to have had issues with custom (or misspelled) validators, but the failures are all for Rails default validators. For example, running rails c gives me:
Users/MY_USER/.rvm/gems/ruby-1.9.3-p547#MY_APP/gems/activemodel-3.2.19/lib/active_model/validations/validates.rb:96 in 'rescue in block in validates': Unknown validator: 'PresenceValidator' (Argument Error)
which traces down to
from /Users/MY_USER/MY_APP/app/models/document.rb:4 in '<class:SomeModel>'
In the model file, I have:
validates :title, presence: true
PresenceValidator is a default Rails validator, and somehow a gem seems to have borked that. I'm not really sure what to do (besides nuking the app from space and reinitializing my development environment).
Can you try this? It worked for me.
validates :title, :presence=>true
Related
I've created a new project with Rails 5.0.0 and Ruby 2.5 on my local mac with macOs 10.12. Now I'm trying to add paperclip to this project but still no luck.
My model class looks like this:
class Photo < ApplicationRecord
has_attached_file :file, styles: { big: '1280x1024>', small: '640x480>' }
validates_attachment :file, content_type: { content_type: /\Aimage/ }, file_name: { matches: [/png\Z/i, /jpe?g\Z/i, /gif\Z/i] }, size: { less_than: 15.megabytes }
end
Whatever version of paperclip I try I don't have a paperclip generator in my project and I'm geting errors like this when I try to call a model or it's methods:
"NoMethodError (undefined method `has_attached_file' for Photo (call 'Photo.connection' to establish a connection):Class)"
When I add "include Paperclip::Glue" like suggested here https://github.com/thoughtbot/paperclip/issues/705 error changes to
NameError (uninitialized constant Photo::Paperclip)
Is there any way for me to bypass this mess ? ><
Link to paperclip issues
https://github.com/thoughtbot/paperclip/issues/2555
Link provided by hamdi in the first comment is an answer.
If you've got problems with Devise or with Paperclip like that, don't try to add "include Paperclip::Glue" or in case of Devise "extend Devise::Models". The only right way to fix this is to rollback ALL of your migrations, terminate console, start it again, migrate, terminate console one more time and thats it! Sounds stupid but it's working >< Pictures upload is working and paperclip generator is on the list.
For all of you who came from Rails 4:
2.5.0 :001 > Photo
=> Photo (call 'Photo.connection' to establish a connection)
is totally ok behavior for console in Rails 5. Before accessing model you've got to perform 'Photo.connection' from now on. If you don't like it you can always fix it by adding
console do
ActiveRecord::Base.connection
end
to your config/application.rb
I am trying to use "has_secure_password" in my Rails project. I have included the bcrypt gem in my Gemfile and ran bundle install. However, when I run the project, I am getting the error below:
NameError in UsersController#index
undefined local variable or method `has_secure_pasword' for #<Class:0x00000004169e38>
User class
class User < ActiveRecord::Base
has_secure_pasword
validates :password, length: { minimum: 6 }
end
Any ideas on how to fix this?
I have not created any users - could that be the issue?
undefined local variable or method `has_secure_pasword' for
Class:0x00000004169e38
has_secure_pasword should be has_secure_password. You are missing a s in password.
We have recently migrated from rails 3.2.3 to Rails 3.2.5 and we are using Devise 2.0.4. In our application we have override the Devise controller just to utilize the omniauth with devise.
When we were using 3.2.3 version on Sign_UP everything was working fine as soon as we migrated to 3.2.5 resource.save stopped working, it does not show any failure or success message, just redirects to sign_in page
After that I tried resource.save! so that I could get the exact erorr, it raised following error message, but this is not true as in the Table there is no such record and even current content is also not saved
Completed 500 Internal Server Error in 1626644ms
ActiveRecord::RecordInvalid (Validation failed: User name has already been taken):
I have also tried it with Devise 2.1 and got the same result.
I have found the problem area. But it is little strange
In our current working Rails 3.2.3 app and we have this validation in our model, this is failing in the Rails 3.2.5
validates :user_name, :uniqueness => true
Reason is - User_name in the table as "nil" already available, and when next time we try to insert "user_name" as NIL it is checking for uniqueness of NIL and there it fails.
To fix this I have added :allow_nil option, but still my question is why did not it fail for rails 3.2.3
validates :user_name, :uniqueness => true, :allow_nil => true
New to Ruby on Rails and having a problem when following Michael Hartl's tutorial.I'm using Rails 3.2.2 with Ruby 1.9.3. The issue looks very similar to another question that was raised but was unanswered:
Rails Error NoMethodError in UsersController#show error
I get the following error when attempting to add a new user via /signup
Gem::LoadError in UsersController#new
bcrypt-ruby is not part of the bundle. Add it to Gemfile.
Reloading the page gives the error:
NoMethodError in UsersController#new
undefined method `key?' for nil:NilClass
The problem seems to be related to the inclusion of the bcrypt-ruby gem, and the usage of the has_secure_password method in user.rb . Removing the call to has_secure_password in user.rb gets rid of the error and it goes to the signup page successfully.
user.rb:
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# password_digest :string(255)
#
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
validates :name, presence: true, length: { maximum: 50 }
valid_email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: valid_email_regex },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6}
end
users_controller.rb:
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
flash[:success] = "Welcome!"
redirect_to #user
else
render 'new'
end
end
end
However, I cant find anything wrong with the inclusion of the bcrypt-ruby gem. In the Gemfile I have:
gem 'bcrypt-ruby', '3.0.1'
and the gem has also been generated in Gemfile.lock :
DEPENDENCIES
annotate (~> 2.4.1.beta)
bcrypt-ruby (= 3.0.1)
I've also added password_digest to the database via migration:
class AddPasswordDigestToUsers < ActiveRecord::Migration
def change
add_column :users, :password_digest, :string
end
end
Any ideas ?
I'm going through the same tutorial and encountered the exact same problem.
My solution was to restart the web server. After installing the gem, I think the web server needs to be restarted so it is loaded.
Justin
Did you tried the 'bundle update' command, usually the bundler will take care of gems if you specified in the Gemfile. If you want to check the gem dependency please check http://rubygems.org/gems.
And if you are using windows(I know its strange- but some of our app works in windows only) there is some tricks to install bcrypt
Steps to install bcrypt.
1 Download Devkit and extract
you can download it from here http://rubyinstaller.org/downloads/
2 Place devkit it your jruby folder (in my case C:\applications\jruby\devkit)
3 You need to install ruby as well either 1.8.7 or 1.9(some times needs a system restart)
4 CD into devkit directory
5 Run ruby dk.rb init
6 Open config.yml and make sure that both your jruby installtion is listed. If not, ADD them. Save and close config.yml after you're done.
example:- C:/applications/jruby
7 Run ruby dk.rb install
8 jruby -S gem install bcrypt-ruby
Restarting the web server fixed it for me (had spork running in the background to speed up the running of the tests)
I attempted to install the gmaps4rails gem.
I added gem 'gmaps4rails' to my Gemfile, and ran 'bundle install. It said that my bundle installed successfully. I can find "Using gmaps4rails (0.8.8)" with 'gem list'. I added the specified columns to my users table with rake db:migrate and added acts_as_gmappable and the gmaps4rails_address method to my User model.
Visiting pages that involve the user model gives me "undefined local variable or method 'acts_as_gmappable'"error.
Is there something that I am missing?
For greater context, the code I am using is from the Rails 3 Tutorial.
OS X 10.6.6
ruby 1.8.7
rails 3.0.7
passenger 3.0.7
Restarting the server should resolve the problem :)
I had the same issue : undefined local variable or method 'acts_as_gmappable'
I just restarted the server and it error went away.
I was using Mongoid and had the same trouble.
In which case, make sure your model includes:
include Gmaps4rails::ActsAsGmappable
acts_as_gmappable :position => :location
field :gmaps, :type => Boolean
field :location, :type => Array
def gmaps4rails_address
#describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
"#{self.address}, #{self.city}, #{self.country}"
end
I think this may be helpful (similar issue):
rvm + rails3 + gmaps4rails - acts_as_gmappable