Helper Module causing friday_night_issue ... RoR - ruby-on-rails

ENV=development
Anyone any thoughts as to why
def tidy_address(location)
t_address=t_address+"<br>"+location.address1 if location.address1.present?
t_address=t_address+"<br>"+location.address2 if location.address2.present?
t_address=t_address+"<br>"+location.address3 if location.address3.present?
t_address=t_address+"<br>"+location.postcode if location.postcode.present?
end
is throwing an error
undefined method `+' for nil:NilClass
I appear to be applying a method to a string , but im not sure how to resolve this
also tried
... +"<br>#{location.field}" ...
Boozy suggestions accepted

Doooh ...
so this resolved it - im an old ASP hack and it was lazy
def tidy_address(location)
t_address="<br>"+location.address1 if location.address1.present?
t_address=t_address+"<br>"+location.address2 if location.address2.present?
t_address=t_address+"<br>"+location.address3 if location.address3.present?
t_address=t_address+"<br>"+location.postcode if location.postcode.present?
end

Related

Redis 3.0.3 rails undefined method `set'

So I'm simply trying out redis on ruby on rails
and I keep getting this error .
undefined methodset' for nil:NilClass`
On my model I created this method
def self.create_bookmark(bookmark)
$redis.set('redis','so its working')
end
then simply call it on my controller.
I, have tried the command on my rails console.
irb(main):022:0>$redis.set('party','Now!!')
irb(main):022:0>=>"OK"
irb(main):022:0>$redis.get('party')
irb(main):022:0>=>"NOW!!"
The commands seems to be working fine on the console.
Did you forget to give your method a name?
def my_method
$redis.set('redis','so its working')
end

undefined method [] for nil:NilClass when trying to pull a value of a hash - inspect shows it's there

So, I was migrating a rails app to a newer version of the gems, ruby version etc, and I ended up with one problem:
I have a hash #values that I pull from in my controller, and whenever i try to do so, I get
undefined method [] for nil:NilClass
But when I throw in a logger to grab the value via #values.inspect I can clearly see the keys and values. I am sure it must be something very obvious I am missing, and I found another user that had this problem and solved it, but I can't seem to make it work. The full code is
if #values[:true_value]
# do some stuff
end
And I get the undefined method [] for nil:NilClass error on the if line.
Thank you for taking the time to help me out
EDIT:
I have a method:
def GH
{
:true_value=>true
}
end
So when using it in the controller, I load #values as:
#values = GH
try present..just like we do usually for checking params
if #values[:true_value].present?
# do some stuff
end
You can't have methods that begin with a capital letter; these are reserved for constants.
Rewrite your method as:
def gh
{ :true_value => true }
end
on 3 lines (not one line as in your question) and
#values = gh
method name must be in small, instead of GH it should be gh
def gh; { :true_value=>true }; end
#values = gh
#values[:true_value]
=> true
I hope it will solve your problem.

Upgraded to ActiveAdmin 1.0. Getting error undefined method `exclude_contest_eq' for Ransack::Search

I just upgraded to rails 4 and ActiveAdmin 1.0. I'm having problem with page that won't work anymore. Here is the error:
undefined method `exclude_contest_eq' for Ransack::Search<class: Project, base: Grouping <combinator: and>>:Ransack::Search
Here is where the code is failing
controller do
def render(*args)
#projects.uniq! if #projects and action_name == 'index'
super(*args)
end
end
What has changed that is throwing this error?
It has something to do with filtering.
Here is the thread with long discussion of the issue.
The essence of it, that quickest fix would be, probably, to add remove_filter :exclude_contest to the model definition.
May this will solve your problem as well.

Twitter Bootstrap error when running 'bootstrap:themed', with Mongo

This occurs when I try to run rails g bootstrap:themed Associations in my terminal:
C:/Users/ruby/.pik/rubies/Ruby-193-p327/lib/ruby/gems/1.9.1/gems/twitter-bootstrap-rails-2.1.9/lib/generators/bootstrap/themed/themed_generator.rb:87:in `block in retrieve_columns': undefined method `columns' for Association:Class (NoMethodError)
It just can't seem to work, I've tried many ways, searched everywhere, never succesful. I'm using Mongo.
I just got exact same error. I created two identical project - one with mongoid and one without. I only get the error on the mongoid project.
Found this workaround that seems to solve the problem:
Remove references to ActiveRecord (around line 87) in the file:
/home/ubuntu/.rvm/gems/ruby-1.9.3-p327/bundler/gems/twitter-bootstrap-rails-b8b7eb22614a/lib/generators/bootstrap/themed/themed_generator.rb
I changed ...
def retrieve_columns
if defined?(ActiveRecord)
rescue_block ActiveRecord::StatementInvalid do
#model_name.constantize.columns
end
else
rescue_block do
#model_name.constantize.fields.map {|c| c[1] }
end
end
end
to this ...
def retrieve_columns
rescue_block do
#model_name.constantize.fields.map {|c| c[1] }
end
end
To get the views working I needed to make sure that my model class had a created_at field that wasn't nil (alternatively edit the generated views).
Hope this helps.
PS: Wow ... it seems you've got twitter-bootstrap-rails working on windows - I didn't know that was possible!

NoMethodError User Authentication

I'm trying out user authentication for the first time and am running to a bit of an issue.
I've defined my make_salt method in my AdminUser model
def self.make_salt(username="")
Digest::SHA1.hexdigest("Use #{username} and other stuff")
end
Then in my console when I run AdminUser.make_salt, I get:
>> AdminUser.make_salt
NoMethodError: undefined method `make_salt' for #<Class:0x1063ddb58>
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.7/lib/active_record/base.rb:1009:in `method_missing'
from (irb):30
Also, I had a hash method defined, which worked fine, but when I changed it to hash_with_salt I also got a NoMethodError.
Am I just missing something obvious here?
Ugh. Finally figured out this one. I simply needed to restart my console after saving the changes to my code.

Resources