Rails server says it cannot load the bcrypt gem? - ruby-on-rails

When I try to do this action I get the "LoadError (cannot load such file -- bcrypt)"
require 'bcrypt'
class PublicController < ApplicationController
def attempt_login
if params[:email].present? && params[:password].present?
found_user = User.where(:email => params[:email]).first
if found_user
authorized_user = found_user.authenticate(params[:password])
end
end
if authorized_user
render(:text =>'authorized')
redirect_to(:controller => 'private', :action => 'home')
else
flash[:notice] = "Invalid email or password"
render :new
end
end
end
User model:
class User < ApplicationRecord
has_secure_password
end
I have tried all of the following in the gemfile and I get the same error every time:
gem 'bcrypt', '~>3.1.11'
gem 'bcrypt', '~> 3.1', '>= 3.1.11'
gem 'bcrypt', platforms: :ruby
How can I get it to load the bcrypt gem?
I'm using Windows 10.

Try this:
Remove bcrypt from your Gemfile.
Uninstall from your system by running gem uninstall bcrypt.
Add gem 'bcrypt', git: 'https://github.com/codahale/bcrypt-ruby.git', :require => 'bcrypt' to your Gemfile.
Run bundle install.
If you get errors, install Git https://git-scm.com/downloads and retry bundle install.
Source: https://github.com/codahale/bcrypt-ruby/issues/102#issuecomment-284118731

Related

While using rspec i am constantly getting the same error

I am getting the same Error on running rspec that is :-
"Undefined Method - authorize_manage_partner_links"
I am using CANCAN gem in my Gemfile for authorization purposes and i am using Rails version 3.2 and Ruby version 1.9.2.
Please suggest what to do as i am totally new to rspec and rails. I am using a testing database, on importing the database for the first time, when i am running rspec it is showing successful but after that it is showing this error on running the same rspec file. I am using FactoryGirl. and i have defined everything in my factoryrb file as well.
This is my controller--
require 'will_paginate/array'
class PartnerLinksController < ApplicationController
#load_and_authorize_resource
before_filter :authorize_manage_partner_links!
layout "backoffice"
def index
#partner_links = PartnerLink.all
if params[:sortby]
#partner_links = #partner_links.sort_by {|p| p.name} if params[:sortby] == "up"
#partner_links = #partner_links.sort_by {|p| p.name}.reverse if params[:sortby] == "down"
else
#partner_links = #partner_links.sort_by {|p| p.name}
end
total = #partner_links.size
#partner_links = #partner_links.paginate(:page => params[:page], :per_page => 20)
page = params[:page]
if !page
page = 1
end
render :locals => {:total => total, :page => page, :sortby => params[:sortby], :partner_link => #partner_link}
end
def new
#partner_link = PartnerLink.new
end
def edit
#partner_link = PartnerLink.find(params[:id])
end
def create
#partner_link = PartnerLink.new(params[:partner_link])
if #partner_link.save
redirect_to partner_links_url
else
render "new"
end
end
def update
#partner_link = PartnerLink.find(params[:id])
if #partner_link.update_attributes(params[:partner_link])
redirect_to partner_links_url
else
render "edit"
end
end
def destroy
#partner_link = PartnerLink.find(params[:id])
#partner_link.destroy
redirect_to partner_links_url
end
end
This is my rspec controller that i am using--
require 'spec_helper'
describe PartnerLinksController do
before do
FactoryGirl.create(:partner_link)
role_permission
admin_role
user
currency
category
merchant
cart
deal
sign_in user
end
let(:role_permission) {FactoryGirl.create(:role_permission)}
let(:currency) {FactoryGirl.create(:currency)}
let(:merchant) {FactoryGirl.create(:merchant)}
let(:cart) {FactoryGirl.create(:cart)}
let(:deal) {FactoryGirl.create(:deal)}
let(:category) {FactoryGirl.create(:category)}
let(:authentication) {FactoryGirl.create(:authentication)}
let(:user) {FactoryGirl.create(:user)}
let(:admin_role) {FactoryGirl.create(:admin_role)}
describe "index" do
it "Should display all the partner links" do
get :index
end
end
describe "new" do
it "Should create all the partner links" do
get :new
end
end
describe "create" do
it "Should create new all the partner links" do
get :create
end
end
describe "update" do
it "Should update all the partner links" do
get :update
end
end
describe "destroy" do
it "Should delete all the partner links" do
get :destroy
end
end
end
app/models/partner_link.rb--
class PartnerLink < ActiveRecord::Base
# attr_accessible :title, :body
validates_presence_of :name, :url, :description
end
This is my Gemfile--
source 'http://rubygems.org'
ruby '1.9.2'
gem 'rails', '3.2.11'
gem 'authlogic'
gem "devise"
gem "devise-encryptable"
gem "devise-async"
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem "cancan"
gem 'activemerchant'
gem 'aws-sdk'
gem 'aws-s3'
gem 'paperclip', '~> 3.0'
gem 'oauth2'
#gem 'fastercsv' #no-longer required after ruby 1.9
gem 'pg'
#gem 'thin'
gem 'unicorn'
gem 'simple_form'
gem 'haml-rails' # Use HAML instead of ERB for templates
gem 'friendly_id'
gem 'will_paginate'
gem 'will_paginate-bootstrap'
gem "nifty-generators", :group => :development
#gem "pdfkit"
gem "pdfkit", "~> 0.5.0"
gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'
gem 'sendgrid', "~> 1.0.1"
gem 'delayed_job'
gem 'delayed_job_active_record'
gem 'bootstrap-datepicker-rails'
#gem 'roadie' #fixing email css
group :development, :test do
gem 'rspec-rails'
gem 'guard-rspec'
# Database Cleaner clears the database between tests. This done because we have
# to set config.use_transactional_fixtures = false in the spec_helper file.
# The use_transactional_fixtures value is set to false because Selenium test
# driver cannot access the test records created via database transactions.
gem 'database_cleaner'
end
group :test do
# gem 'factory_girl_rails'
gem 'capybara'
# As we’re using Capybara we can call the save_and_open_page method at any
# point and this will open the page in a browser so that we can take a look
# at it. This is enabled by Launchy.
# gem 'launchy'
# gem 'rspec-mocks'
end
gem "less-rails"
gem 'twitter-bootstrap-rails', '>= 2.0.3'
gem 'rb-readline', '~> 0.5.0', require: 'readline'
gem 'execjs'
gem 'therubyracer'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', " ~> 3.2.3"
gem 'coffee-rails', "~> 3.2.1"
gem 'uglifier', '>= 1.0.3'
end
gem "mail", "~> 2.4.4"
gem 'jquery-rails'
#gem "mocha", :group => :test
gem 'gmaps4rails'
gem 'acts_as_xlsx'
gem "httparty"
gem 'newrelic_rpm'
gem 'font_assets'
gem 'paper_trail', '~> 3.0.6'
gem 'mailchimp-api', require: 'mailchimp'
gem 'rack-ssl-enforcer'
gem 'ckeditor'
gem "rails_best_practices"

SQLite3: no such column: "id desc"

I'm working on a piece of very old code (ruby 1.9.3 and rails 2.3.12). I'm trying to write some tests for it so that when I start upgrading it I have an idea of how everything is working. I'm writing them in cucumber at the moment and have come across an issue that I can't seem to solve. This issue does not appear in the production version as everything works fine on that. In the development AND test however, when I try and click one of the links, the following error appears;
And I click "Trips (1)" # features/step_definitions/user_management_steps.rb:1
SQLite3::SQLException: no such column: "id desc": SELECT "trips".* FROM "trips" INNER JOIN "trips_users" ON "trips".id = "trips_users".trip_id WHERE (("trips_users".user_id = 1)) ORDER BY ["id desc"] LIMIT 10 OFFSET 0 (ActiveRecord::StatementInvalid)
./vendor/plugins/geokit-rails/lib/geokit-rails/acts_as_mappable.rb:157:in `find'
./app/controllers/users_controller.rb:259:in `trips'
/home/camillavk/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
<internal:prelude>:10:in `synchronize'
./features/step_definitions/user_management_steps.rb:2:in `/^I click "([^"]*)"$/'
features/trips.feature:30:in `And I click "Trips (1)"'
I've looked everywhere and cannot find any reference to
no such column: "id desc"
does anyone know what this means?
And why does this break in the development and test environments but not in the production environment?
Any help would be greatly appreciated!
*Edit
gemfile;
source 'https://rubygems.org'
gem "rails", "~> 2.3.12"
gem "airbrake"
gem "aws-s3"
gem "bj"
gem "calendar_date_select", "1.15"
gem "cancan", "1.2.0"
gem "carmen"
gem 'comma', '0.4.1'
gem 'exifr'
gem "factory_girl", "1.2.4"
gem 'fastercsv'
gem "geokit"
gem "haml"
gem "hpricot"
gem 'hoptoad_notifier'
gem "jrails"
gem "libxml-ruby", "2.4.0"
gem "memcached", "~> 1.8.0"
gem "mysql2", "~> 0.2.6"
gem "rake", "0.8.3"
gem 'recurly'
gem "rdoc"
gem "rmagick"
gem "rspreedly"
gem "rubyzip"
gem 'sass'
#need to use simplecov when upgrading to ruby 2 as rcov doesn't work
gem "simplecov"
gem 'sqlite3'
gem "will_paginate", "2.3.16"
gem 'yaml_db'
group :development, :test do
gem 'test-unit', '1.2.3'
gem 'rspec', '1.3.2'
gem 'rspec-rails', '~> 1.3.4'
end
group :test do
gem 'capybara', '0.3.5'
gem 'cucumber', '1.1.2'
gem 'cucumber-rails', :git => "git://github.com/RKelln/cucumber-rails.git", :branch => "0.3.2-capybara-fix"
gem 'database_cleaner'
end
user_controller.rb;
class UsersController < ApplicationController
before_filter :find_user
load_and_authorize_resource :except => [:news, :search, :update, :mobile_new, :mobile_success]
before_filter :authorise, :only => [:export_to_csv]
def find_user
begin
#user = User.find_by_login(params[:id])
rescue
#user = User.find(params[:id])
end
end
....
....
def trips
body_attributes "view-followers", 'main-feed', 'Trips'
#page = (params[:page].to_i || 0)
#page = 1 if #page == 0
#trips = #user.trips.reverse_order
#year = ''
#month = ''
unless params[:year].blank?
#year = params[:year].to_i
#trips = #trips.select { |trip| trip.start_date.year == #year }
end
unless params[:month].blank?
#month = params[:month].to_i
#trips = #trips.select { |trip| trip.start_date.month == #month }
end
#years = Array.new
unless #user.trips.empty?
#years = #user.trips.first.start_date.year..#user.trips.last.start_date.year
end
#show_more_link = #trips.count > (10 * #page)
#trips = #trips.paginate(:per_page => 10, :page => #page)
end
It looks like you have an order on your Trips query defined like this Trip.order('[id desc]'). The correct format is (depending on your Rails version):
Trip.order('id DESC')
Trip.order(id: :desc) # works only in Rails >= 4.0

Errno::ECONNREFUSED (Connection refused... in production environment

I have finished my app and I am trying to check the production environment.
The app uses sunspot_rails gem for search. It works fine on the development server but it does not work on the production environment. My production logs gives the following:
Errno::ECONNREFUSED (Connection refused - {:data=>"fq=type%3AArticle&start=0&rows=1&q=%2A%3A%2A", :method=>:post, :params=>{:wt=>:ruby}, :query=>"wt=ruby", :headers=>{"Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"}, :path=>"select", :uri=>#<URI::HTTP:0x007ff38e350578 URL:http://localhost:8983/solr/production/select?wt=ruby>, :open_timeout=>nil, :read_timeout=>nil, :retry_503=>nil, :retry_after_limit=>nil}):
app/controllers/articles_controller.rb:12:in `index'
How do I solve this issue ?
Here is my articles_controller:
class ArticlesController < ApplicationController
before_filter :require_login, except: [:show, :index]
def index
#search = Article.search do
fulltext params[:search]
paginate :page => params[:page], :per_page => 1
end
#articles = #search.results
##articles_by_month = Article.find(:all).group_by { |post| post.created_at.strftime("%B") }
end
def show
#article = Article.find(params[:id])
#comment = Comment.new
#comment.article_id = #article.id
end
def new
#article = Article.new
end
def create
#article = Article.new(article_params)
#article.save
redirect_to article_path(#article)
end
def edit
#article = Article.find(params[:id])
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
def update
#article = Article.find(params[:id])
#article.update(article_params)
flash.notice = "Article '#{#article.title}' Updated!"
redirect_to article_path(#article)
end
def article_params
params.require(:article).permit(:title, :body, :tag_list, :picture)
end
end
And here is my Gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.8'
gem 'bcrypt','3.1.7'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sprockets', '2.11.0'
gem 'carrierwave', '0.10.0'
gem 'mini_magick', '3.8.0'
gem 'fog', '1.23.0'
gem 'sorcery'
gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'sunspot_rails'
gem 'progress_bar'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
gem 'sunspot_solr'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '4.0.3'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
How do I solve this issue ?
Have you started the solr instance in your production server?
bundle exec sunspot-solr start -p 8983
I dealt with some similar issues with Solr in the past. I made a script to handle launching it for me. It makes sure any crufty files that may affect access are cleaned up.
Hope this helps out!
See below:
pkill -f solr
#remove old data
rm -rf solr/data
rm -rf solr/default
rm -rf solr/development
rm -rf solr/pids
rm -rf solr/test
while getopts 'pdt' flag; do
case "${flag}" in
d) export RAILS_ENV=development ;;
p) export RAILS_ENV=production ;;
t) export RAILS_ENV=test ;;
*) error "Unexpected option ${flag}" ;;
esac
done
#startup solr
bundle exec rake sunspot:solr:start
echo "Solr started successfully."

Mass assignment error when creating first user for monologue rails blog engine

I am using the monologue blog engine for an existing rails 4 app. I'm following their instructions via their github page and have encountered a mass assignments error when creating the first user in the rails console. I would typically go into the model and allow mass assignments, however I can't seem to find any model files for monologue in my rails application. When I navigate to /blog on my site, I see the template, and when I goto /blog/login, I see the login page. I should also note, I've copied the contents of the monologue views, models, controllers, helpers etc to my monologue files. The users_controller.rb file includes a protected method that should allow mass assignment.
The error is:
WARNING: Can't mass-assign protected attributes for Monologue::User: name, email, password, password_confirmation
I've included monologue in my gemfile:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '4.1.0'
gem 'devise', '3.0'
gem 'google-analytics-rails'
gem 'meta-tags'
gem 'databasedotcom'
gem 'databasedotcom-rails'
gem 'protected_attributes'
gem 'thin'
group :development do
gem 'pg'
end
group :production do
gem 'newrelic_rpm'
gem 'rails_12factor'
end
gem 'sass-rails', '~> 4.0.2'
gem 'bootstrap-sass'
gem 'sprockets', '2.11.0'
gem 'sprockets-rails'
gem 'coffee-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'jquery-rails', "~> 2.1.0"
gem 'font-awesome-rails'
gem 'monologue', github: 'jipiboily/monologue'
users_controller.rb in app/controllers/monologue/admin/users_controller.rb
class Monologue::Admin::UsersController < Monologue::Admin::BaseController
before_filter :load_user, except: [:index, :new, :create]
def edit
end
def new
#user = Monologue::User.new
end
def update
if #user.update user_params
flash.notice = "User modified"
redirect_to admin_users_path
else
render :edit
end
end
def destroy
if #user.destroy
redirect_to admin_users_path, notice: I18n.t("monologue.admin.users.delete.removed", user: #user.name)
else
redirect_to admin_users_path, alert: I18n.t("monologue.admin.users.delete.failed", user: #user.name)
end
end
def create
#user = Monologue::User.new user_params
if #user.save
flash.notice = I18n.t("monologue.admin.users.create.success")
redirect_to admin_users_path
else
render :new
end
end
def index
#users = Monologue::User.all
end
private
def load_user
#user = Monologue::User.find(params[:id])
end
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
My Routes.rb file:
# Blog URL
mount Monologue::Engine, at: '/blog' # or whatever path, be it "/blog" or "/monologue"
Open you application.rb file and change---
config.active_record.whitelist_attributes = true
to
config.active_record.whitelist_attributes = false
and restart server.

ActiveAdmin - can't create model "Rating"

I have no idea why I get the error when I click on "New Rating". And it seems creation of other models gives the same error.
Here's the error msg: "no member 'rating' in struct"
The error is on line 316 of inherited_resources-1.4.0/lib/inherited_resources/base_helpers.rb
Full stacktrace: https://gist.github.com/depy/5330548
313 # extract attributes from params
314 def build_resource_params
315 parameters = respond_to?(:permitted_params) ? permitted_params : params
316 rparams = [parameters[resource_request_name] || parameters[resource_instance_name] || {}]
317 if without_protection_given?
318 rparams << without_protection
319 else
320 rparams << as_role if role_given?
321 end
Here's my Rating model:
class Rating < ActiveRecord::Base
validates_uniqueness_of :task_id, :scope => [:user_id, :user_role], :message => 'You have already rated this task'
validates_inclusion_of :user_role, :in => %w( client contractor )
validates_presence_of :task_id, :user_id
validates_presence_of :score, :message => 'Please rate your satisfaction'
belongs_to :task
belongs_to :user
end
Here's my ActiveAdmin Rating.rb:
ActiveAdmin.register Rating do
controller do; include StrongAdmin; end
# list
index do
column :user_id
column :task_id
column :comment
column :score
default_actions
end
# single
show do |rating|
attributes_table do
row :id
row :rater do
task_id = Rating.find(params[:id]).task_id
div do
Task.find(task_id).client.full_name + " [" + Task.find(task_id).client.id.to_s+"]"
end
end
row :user
row :user_role
row :task
row :score
row :comment
row :created_at
end
active_admin_comments
end
end
Here's my Gemfile:
source 'https://rubygems.org'
ruby "1.9.3"
gem 'rails'
gem 'pg' # Database (PostgreSQL)
group :assets do
gem 'sass-rails'
gem 'uglifier'
gem 'bourbon'
gem 'jquery-fileupload-rails'
gem 'jquery-rails'
gem 'asset_sync'
end
group :production do
gem 'honeybadger'
gem 'newrelic_rpm' # App monitoring
end
gem 'bcrypt-ruby' # Secure passwords
gem 'devise' # Authentication
gem 'unicorn' # Webserver
gem 'redis'# Session storage and Task applications
gem 'carrierwave' # File uploading
gem 'mini_magick' # Image processing
gem 'kaminari'# Pagination
gem 'turbolinks' # Pushstate and async loading
gem 'strong_parameters' # Attribute accessible replacement
gem 'dalli' # Memcached interface
gem 'fog' # CDN connector
gem 'activeadmin' # (user) administration
gem 'cancan' # Authorization
gem 'gibbon' # Mailchimp API wrapper
gem 'multi_json'
gem 'nokogiri' # HTML parser for error messages
gem 'pusher' # Hosted websockets
gem 'sidekiq' # Async processing
gem 'draper' # Decorator
gem 'lograge' # Improved logger
gem 'slim'
gem 'sinatra', :require => nil
group :development do
gem 'sqlite3'
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
gem 'quiet_assets'
end
group :test, :darwin do
gem 'rb-fsevent'
end
group :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'capybara'
# gem 'capybara-webkit'
gem 'database_cleaner'
gem 'launchy'
gem 'faker'
end
I encountered the same issue. I updated ActiveAdmin from 0.5.1. to 0.6.0. The inherited resource gem was also updated to 1.4.0.
Since ActiveAdmin requires 1.3.1 or higher, you can lock the version in your gem
gem 'inherited_resources', "1.3.1"
This solved the issue for me.

Resources