undefined method `synchronize' for nil:NilClass - ruby-on-rails

I just created a new app from scratch on rails4.0.0.rc1 with 1.9.3-p374 (which should work according to the guides).
However, I fail doing a simple create-action.
class Books
def create
#book = Book.new book_params
if #book.save
redirect_to #book
else
render action: :new
end
end
private
def book_params
params.require(:book).permit(:name, :description)
end
end
Doing a create here results a
undefined method `synchronize' for nil:NilClass
with the error pointing to the line of if #book.save.
Is that because of 1.9.3 or did I miss something?

Apparently, It was failing because of the friendlyId gem. If you use friendlyId, you may need to specify the rails4-branch.
gem 'friendly_id', github: 'FriendlyId/friendly_id', branch: 'rails4'
edit: Make sure to get the actual branch/version from norman/friendly_id. According to the readme, it needs to be at least branch: 5.0.0-beta to work with rails4.
edit2: now it is 5.0.0.rc2, as I said, make sure to get the actual branch/version, I won't obviously update this answer every time there is a version update. Also, read the comments to this answer, there are plenty of helpful information.

pduersteler's answer didn't work for me and failed with this error:
fatal: ambiguous argument 'rails4': unknown revision or path not in the working tree.
The README was update on Sep 23, 2013 with the following, which installed without error:
gem 'friendly_id', '5.0.0.rc2' # Note: You MUST use 5.0.0 or greater for Rails 4.0+

Related

Pagination issue: `map' : undefined method 'existent'

I just installed the will_paginate, 3.0.7 and bootstrap-will_paginate, 0.0.10 gem and when i call the following to get my articles paginated
def index
#articles = Article.paginate(page: params[:page], per_page: 5)
end
I get the following error upon attempting to launch the localhost server
/Users/Jack/.rvm/gems/ruby-2.3.1#global/gems/activesupport-5.0.0.1/lib/active_support/i18n_railtie.rb:45:in `map': undefined method `existent' for #<String:0x007fd4a2bfa5d0> (NoMethodError)
Did you mean? extend
What's going wrong?
This was an issue with earlier version of will_paginate with Rails 5.
You need to update your will_paginate gem to use latest updated one which is:
gem 'will_paginate', '3.1.5'
This issue has been fixed with this merge:
https://github.com/mislav/will_paginate/pull/450

Rails 5 install error (DEPRECATION WARNING: alias_method_chain is deprecated)

I'm running into this error after installing rails 5.0.0 on my project. I'm starting to think it's Devise gem or something. I tried multiple ways, but can't seem to figure it out.
I've tried this for devise gem, but same result.
gem 'devise', :github => 'plataformatec/devise', :branch => 'master'
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /home/nitrous/code/uvesty/config/environment.rb:5)
Exiting
/home/nitrous/code/uvesty/.bundle/gems/actionpack-5.0.0/lib/action_dispatch/middleware/stack.rb:108:in `assert_index': No such middleware to insert after: ActionDispatch::ParamsParser (RuntimeError
I also thought I might need to change all my
#user = User.find(params[:id])
to
#user = User.find_by(id: params[:id])
First change devise declaration in Gemfile like
gem "devise", '~> 4.0.0.rc1'
Secondly, you don't need to change this line, as find method is NOT deprecated.
#user = User.find(params[:id])
Please check what new in rails 5
The issue was the gem rails_admin. I removed it and it's all fine now. It seems they might have not updated their gem for rails 5.0.

Whois gem not working in rails

class DomaincheckerController < ApplicationController
def index
end
def store
r =Whois.whois(secure_params['domain'])
render :text => "#{r}"
end
private
def secure_params
params.require(:whois).permit(:domain)
end
end
This is my domainchecker controller. The index method renders a form. After submitting the form it goes to store method. Here I am trying to use the whois gem. I have installed whois gem by running gem install whois. But I am getting this error.
uninitialized constant DomaincheckerController::Whois
The problem is that you installed the gem directly and not using bundler, therefore the Rails app can't find the dependency.
In order to install a gem in a Rails project you need to edit the Gemfile file and add the gem there. Once added, run
$ bundle
in order to install the dependency. Check the documentation about the Gemfile.

ENOENT Error in Serializer

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'

numbers_and_words gem not working - NoMethodError in Posts#index - undefined method `to_words' for 42:Fixnum

I would like to convert some numbers in words.
So I installed the numbers_and_words gem with "gem install numbers_and_words" -
I restarted the server, and tried to run this example from the Read.me in my index.html.erb:
<%= 42.to_words %>
but I get this error:
NoMethodError in Posts#index -
undefined method `to_words' for 42:Fixnum
I checked the gem documentation a few times, but I couldn't figure out, what I am missing.
This is my posts controller regarding index.
def index
#posts = Post.order("created_at desc")
#published = Post.where(draft:false).order("created_at desc")
#drafts = Post.where(draft:true).order("created_at desc")
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
What did I wrong? Did I forget something in the installation process?
I am quite new to rails, sorry if this is a trivial newbie question.
Thank you so much for your help! Really appreciated.
Installing a gem is not sufficient to make it available in a Rails project. You need to add it to the Gemfile so that you can manage the dependencies with Bundler.
Edit the 'Gemfile' and add the gem
gem 'numbers_and_words'
Then run bundle again to update the Gemfile.lock
$ bundler
This will make the gem available to the app. It will also autorequire the gem on boot, if the gem uses the standard naming conventions.
It seems this gem is name correctly. Otherwise, you can explicitly require it by setting a require option in the gemfile
gem 'numbers_and_words', require: 'numbers_and_words'
If you just installed the gem locally and you didn't configure the Gemfile, the application will crash once you will deploy it.
Found a solution - not mentioned in the documentation of the gem, but it did the trick for me:
I added
require 'numbers_and_words'
in the rake file. After server restart, it's working.

Resources