I have just started working with rails and I encountered this error which does not give a lot of detail. Since I am not familiar with ruby on rails, perhaps someone here can help.
The error occurs in the active model serializer for the model.
class SecuritySerializer < ActiveModel::Serializer
attributes :id, :name, :ticker, :identifier, :weight
end
the rendering occurs as follows:
def index
#securities = Security.all
render(json: #securities, each_serializer: SecuritySerializer)
end
The error that I get:
Errno::ENOENT (No such file or directory # rb_sysopen - C):
app/serializers/security_serializer.rb:1:in `<top (required)>'
app/controllers/securities_controller.rb:9:in `index'
EDIT
I am using 64-bit ruby on windows 8.
I added this to a file called serializer_init.rb in config/initializers
ActiveModel::Serializer.config.adapter = :json_api
I was using version 0.10.0. I downgraded to 0.8.0 and removed the initializer. This solved the issue.
User gem from branch master.
gem 'active_model_serializers', :git => 'git://github.com/rails-api/active_model_serializers.git'
The following spec passes fine in Ruby 2.1.5 but fails in 2.2.0 and I can't tell what that's all about:
# job.rb
class Job < ActiveRecord::Base
validates :link, :url => true
end
# job_spec.rb
require 'rails_helper'
describe Job do
describe "#create" do
["blah", "http://", " "].each do |bad_link|
it {
should_not allow_value(bad_link).for(:link)
}
end
end
end
fail log looks like this:
1) Job#create should not allow link to be set to "http://"
Failure/Error: should_not allow_value(bad_link).for(:link)
Expected errors when link is set to "http://",
got no errors
# ./spec/models/job_spec.rb:14:in `block (4 levels) in <top (required)>'
I find the only way to for that spec to pass with Ruby 2.2.0 is to include the validates_url gem in my project!!
Does anyone know this is about?
Maybe my solution isn't ideal, but it works.
Replace validates_url gem by validates gem. It has UrlValidator (written by me), which is well tested.
gem 'validates' # in Gemfile
validates :link, :url => true # you needn't to change something. Just remove validates_url from your Gemfile
P.S. It's a strange way - to test functionality of gem. Functionality should be tested in gem already.
P.P.S. I'm strongly recommend you to move to ruby 2.2.1 (or 2.2.2) instead of 2.2.0, because of 2.2.0 has a lot of bugs
I recently upgraded to rails 4.2 and I found that friendly ID stopped working. Not sure if this a bug or if I am literally just failing at using friendly id.
After the update my tests started failing, for example I have the following test:
context "Fiendly ID" do
it "should find by name" do
permission = FactoryGirl.create(:can_read)
Xaaron::Permission.find(permission.permission_name.parameterize).should_not eql nil
end
end
This test never use to fail but now its spitting out:
Failure/Error: Xaaron::Permission.find(permission.permission_name.parameterize).should_not eql nil
ActiveRecord::RecordNotFound:
Couldn't find Xaaron::Permission with 'id'=can_read2
# ./.bundle/gems/gems/activerecord-4.2.0/lib/active_record/core.rb:154:in `find'
# ./spec/models/xaaron/permission_spec.rb:21:in `block (3 levels) in <top (required)>'
With that in mind here is my model:
module Xaaron
class Permission < ActiveRecord::Base
extend FriendlyId
friendly_id :permission_name, use: [:slugged, :finders, :history]
has_many :roles_permissions
has_many :roles, :through => :roles_permissions
validates :permission_name, presence: true, uniqueness: true
def should_generate_new_friendly_id?
permission_name_changed?
end
end
end
notice the :finders. I am running 5.0.3 for Friendly ID. Is this something new with active record or have I failed at using Friendly ID?
The finders module is compatible with Rails 4.2. only in the 5.1. version (not yet released). You can of course already test the version but keep in mind that it's still in beta.
gem "friendly_id", "5.1.0.beta.1"
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 am running:
Ruby 1.9.3p0,
Rails 3.1.1,
Devise 1.4.9,
Devise_ldap_authenticatable 0.4.10
I am using Devise to authenticate my Rails application via an ldap server. I am using username instead of email to authenticate, so naturally the email field in my table is blank.
To query the ldap for email, the official way is to add this code in the user model:
before_save :get_ldap_email
def get_ldap_email
self.email = Devise::LdapAdapter.get_ldap_param(self.username,"mail")
end
This code fails, without attempting to do anything with the ldap, with this:
undefined method `mail' for nil:NilClass
It refers to the line inside the method definition. The log output is no more helpful:
Started POST "/users/sign_in" for 10.50.1.96 at 2011-11-15 11:18:16 -0800
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"<hidden>=", "user"=>{"username"=>"<hidden>", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."username" = '<hidden>' LIMIT 1
LDAP: LDAP dn lookup: uid=<hidden>
LDAP: LDAP search for login: uid=<hidden>
LDAP: Authorizing user <hidden>
LDAP: LDAP dn lookup: uid=<hidden>
LDAP: LDAP search for login: <hidden>
Completed 500 Internal Server Error in 251ms
NoMethodError (undefined method `mail' for nil:NilClass):
app/models/user.rb:14:in `get_ldap_email'
All lines previous to the 500 error are normal LDAP successful authentication that are unrelated to the the email query.
I started learning Ruby, Rails, and Devise just last week, so I'm not sure what files would be the most telling, but here is my user.rb model and gemfile:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :ldap_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
before_save :get_ldap_email
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :username, :password, :password_confirmation, :remember_me
def get_ldap_email
self.email = Devise::LdapAdapter.get_ldap_param(self.username,"mail")
end
end
And the gemfile:
source 'http://rubygems.org'
gem 'rails', '3.1.1'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
<... unrelated ...>
gem 'therubyracer', :platforms => :ruby
gem "devise"
gem "devise_ldap_authenticatable"
I have tried restarting the server, and have done a bundle install since the last GemFile update. My configuration has ldap_create_user = true and username is the correct field name in users. Is there an error in that method? Could there be a version incompatibility? I'm not really sure what else to check, and rails is giving me nothing beginner-friendly to go on. I would love some help with this.
I'm also having this problem - my current temporary solution is to fetch the data myself using Net::LDAP instead of the ldap_authenticatable classes. The more permanent solution would, of course, be to patch ldap_authenticatable, which I may try to do next.
The issue (at least for me) was this: After poring through the ldap_authenticatable code (namely ldap_adapter.rb) I discovered that the get_ldap_param method is not authenticating with the server when trying to fetch the params unless admin_user and admin_password are specified in ldap.yml. So, if your LDAP server allows anonymous reading of data, then get_ldap_param will work as advertised. On OpenLDAP (which is what I use for local testing), anonymous read access is set with the "access to" property in slapd.conf:
access to *
by anonymous auth
But, my corporate LDAP does not allow that.
The Net::LDAP instance in ldap_authenticatable needs to be created with auth parameters when being used for parameter fetching, but it's not. No auth parameters are given, so no results are returned.
So, temporarily, I have the following code in my User model, calling get_ldap_data as a before_save filter:
def has_ldap_data?
[self.email, self.first_name, self.last_name].none? {|v| v.nil?}
end
def get_ldap_data
return true if has_ldap_data? or self.password.nil?
ldap = create_ldap
ldap.search(
:base => ldap.base,
:attributes => ['cn', 'sn', 'givenname', 'mail'],
:filter => Net::LDAP::Filter.eq('cn', self.username),
:return_result => true) do |entry|
self.email = entry.mail.first
self.first_name = entry.givenname.first
self.last_name = entry.sn.first
end
has_ldap_data?
end
def create_ldap(password = nil)
ldap_config = ldap_config = YAML.load(ERB.new(File.read(::Devise.ldap_config || "#{Rails.root}/config/ldap.yml")).result)[Rails.env]
ldap_options = {
:host => ldap_config["host"],
:port => ldap_config["port"],
:base => ldap_config["base"],
:auth => {
:method => :simple,
:username => "cn=#{self.username},#{ldap_config['base']}",
:password => password || self.password
}
}
ldap_config["ssl"] = :simple_tls if ldap_config["ssl"] === true
ldap_options[:encryption] = ldap_config["ssl"].to_sym if ldap_config["ssl"]
Net::LDAP.new(ldap_options)
end
Modify per your particular needs. It's not ideal, but works for now until forking/patching ldap_authenticatable to account for this use case.
So here's the problem and a potential solution.
ldap_get_param assumes that anonymous read access to the LDAP server is allowed, and thus tried to bind and read with {:method => :anonymous}. If your server does not allow anonymous searches, and eg. Active Directory does not by default, then your call will fail with the obscure error message above, which is really trying to say "no matching results".
Unfortunately, devise_ldap_authenticable does not allow you to require authentication for read operations, so I went and forked a copy that does. To use it instead, slot this into your Gemfile instead of the original:
gem 'devise_ldap_authenticatable', :git => 'https://github.com/jpatokal/devise_ldap_authenticatable.git'
And then call get_ldap_param like this:
Devise::LdapAdapter.get_ldap_param(self.login,"mail",self.password)
Note the 3rd parameter there. I've also submitted this as a pull request to master, and you can track the issue here: https://github.com/cschiewek/devise_ldap_authenticatable/issues/94
I'm afraid I don't got the answer for you, but I can tell you that I'm getting the same exact error as you.
I've done a couple of posts here at StackOverflow but haven't gotten any answers. Everything works fine for me until I try to pull extra attributes and :before_save. I've traced my eDir server with ndstrace and it says the question it gets is empty?! So it seems that my selected attribute is not passed down to the ldap server.
Probably this is related to the version you are using.
Inspecting the git repository of the project, I see that the method get_ldap_param has been introduced with version 0.4.7.
Check that you have no constraints on the version of the devise_ldap_authenticatable gem in your Gemfile, and remove them in the case, then run
$ bundle update devise_ldap_authenticatable
it should update your gem to version 0.6.0 (with devise 2.0.1), and the method is present and working.
You could also specify a minimum version in the Gemfile, if you think it is the case:
gem 'devise_ldap_authenticatable', '~> 0.4.7'