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)
Related
I have an issue including Algolia module in my rails app
I have included gem "algoliasearch-rails", ran bundle install and added in config/initializers an algoliasearch.rb file with api id and key
#model.rb
include AlgoliaSearch
algoliasearch do
attribute :name, :description, :content, :is_public => true
end
I think you should restart your rails server ;)
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.
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'
While working on Daniel Kehoe's Learn Ruby on Rails book, I got to the spreadsheet connection section. I followed the workaround for google drive and I get the message sent flash notice, but when I check Google Drive I am not seeing a created
Learn-Rails-Example
spreadsheet. This is my models/contact.rb file.
require 'google_drive_v0'
class Contact
include ActiveModel::Model
attr_accessor :name, :string
attr_accessor :email, :string
attr_accessor :content, :string
validates_presence_of :name
validates_presence_of :email
validates_presence_of :content
validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
validates_length_of :content, :maximum => 500
def update_spreadsheet
connection = GoogleDriveV0.login(Rails.application.secrets.email_provider_username, Rails.application.secrets.email_provider_password)
ss = connection.spreadsheet_by_title('Learn-Rails-Example')
if ss.nil?
ss = connection.create_spreadsheet('Learn-Rails-Example')
end
ws = ss.worksheets[0]
last_row = 1 + ws.num_rows
ws[last_row, 1] = Time.new
ws[last_row, 2] = self.name
ws[last_row, 3] = self.email
ws[last_row, 4] = self.content
ws.save
end
end
Here is the contacts_controller.rb to show that the Message from 'Contacts' should be sent to my email.
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(secure_params)
if #contact.valid?
#contact.update_spreadsheet
UserMailer.contact_email(#contact).deliver
flash[:notice] = "Message sent from #{#contact.name}."
redirect_to root_path
else
render :new
end
end
private
def secure_params
params.require(:contact).permit(:name, :email, :content)
end
end
When I load the server and then try to submit the contact form i get
Authentication failed for : Response code 403 for post https://www.google.com/accounts/ClientLogin: Error=BadAuthentication
And then this in the terminal:
WARNING: GoogleDriveV0.login is deprecated and will be removed in the next version. Use GoogleDriveV0.login_with_oauth instead. Completed 500 Internal Server Error in 153ms
google_drive (1.0.0) lib/google_drive_v0/session.rb:109:in `rescue in login'
google_drive (1.0.0) lib/google_drive_v0/session.rb:102:in `login'
google_drive (1.0.0) lib/google_drive_v0/session.rb:43:in `login'
google_drive (1.0.0) lib/google_drive_v0.rb:19:in `login'
...learn-rails/app/models/contact.rb:15:in `update_spreadsheet'
...learn-rails/app/controllers/contacts_controller.rb:10:in `create'
I had the same error and whilst the issue for me was that I was using POW to serve up my dev site the steps I took to realise this might assist you.
As you did I hard coded the variables in the update_spreadsheet method and this worked - proving the method and google_drive gem were good.
I then pulled up rails console and did
puts Rails.application.secrets.email_provider_username
which got me my email address and proved Rails could see my environment variables.
Assuming you are on the Rails 4.2 version of the book and have added
gem 'web-console', '~> 2.0'
to your gem file when you try to post the form and get the error you should get a console prompt. Here I ran
puts Rails.application.secrets.email_provider_username
again but this time got nil as an answer which confused me at first.
This lead me to realise I was serving up the site with POW (http://pow.cx - a really cool way to serve up your dev sites) which uses its own .powenv file and so had no access to my variables. I confirmed this by running rails server and connecting via localhost:3000 and I could post without issue.
so adding variables to the .powenv (and removing the file from git by adding to .gitignore) then resolved the issue.
Hopefully some of the troubleshooting steps above may help you as it did me and you can complete the tutorial.
I'm getting an error that acts_as_authentic is undefined for rails 3, ruby 1.9.2
My Gemfile has: gem 'authlogic'
The command "bundle show authlogic" shows the correct path where authlogic is installed
My acts_as_authentic appears in a controller:
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.login_field = :phone
end
end
Let me know if there's anything else that will be helpful, I'm new to rails so I'm not entirely sure what to post.
Thanks
For some reason I always ask questions on StackOverflow just before I figure it out. I needed to restart rails server to get it to work.