Redis 3.0.3 rails undefined method `set' - ruby-on-rails

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

Related

Custom authentication for Blazer (Rails)

Where do I define authenticate! to get custom authentication for Blazer?
I have this (uncommented) line in my config/blazer.yml (as described in the README under Authentication > Other):
before_action: :authenticate!
When I refresh my app, I get this error:
NoMethodError at / undefined method 'authenticate!' for #<Blazer::QueriesController:0x007ffe26447830>
That class is defined by the Blazer gem. Am I supposed to add/redefine its authenticate! method somehow?
I tried adding an initializers/blazer.rb file where I defined:
class Blazer::QueriesController < Blazer::BaseController
def authenticate!
true
end
end
But now, when I click "New Query" in Blazer, I get this error:
NoMethodError in Blazer::Queries#new undefined method `errors' for nil:NilClass
Rails 4.2.5, Blazer 1.8.0
You should add your custom method into application_controller.rb. You then put the name of the method into config/blazer.yml to wire it up.
I would recommend using Devise for authentication. The Devise wiki has a great starting article here. The engine and routes of blazer must also be protected as demonstrated here.

Prismic and Ruby on rails : undefined method `[]' for nil:NilClass when trying to get data from prismic api

I'm quite new to ruby and prismic.
I'm trying to connect on the controller of my ruby on rails project to the prismic api.
their tutorial show the following code :
api = Prismic.api('https://your-repo-name.prismic.io/api')
doc = api.query(Prismic::Predicates.at("document.type", "page"))
content = doc.results
i installed the prismic gem correctly and put my repo name correctly.
When i run my code, i get the following error :
NoMethodError in PageController#page
undefined method `[]' for nil:NilClass
for the line
doc = api.query(Prismic::Predicates.at("document.type", "page"))
I checked the api variable value and i get #<Prismic::API:0x007fbcaaab6418> in my api variable, so i guess it's working fine.
I tried to change the predicates but it's doesn't change anything.
If you know a way to solve this problem i would be very gratefull.
Thanks.
edit: my controller page where the error occurs :
Class PageController < ApplicationController
def page
api = Prismic.api('https://xxx.prismic.io/api')
puts 'api ='
puts api
puts '==was api'
doc = api.query(Prismic::Predicates.at("document.tags", ["conseil"]))
content = doc.results
puts 'content'
#test1 = 'test'#content[0]['id']
end
end

Helper Module causing friday_night_issue ... RoR

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

ActionMailer -- NoMethodError: undefined method X for MyMailer:Class

Question: Why is the method undefined if it's just there?
Details:
I have a very simple mailer class:
class ProductMailer < ApplicationMailer
def sample_email
mail(to: "me#example.com") # I hardcode my own email just to test
end
end
And a very simple call from ProductsController:
def sample_email
ProductMailer.sample_email().deliver_later
redirect_to #product, notice: 'Email was queued.'
end
The email fails to be sent. I am using Sidekiq to process emails in background. The Sidekiq Web UI shows failed jobs in the Tries page and I can see why it failed:
NoMethodError: undefined method `sample_email' for ProductMailer:Class
I tried to rename the method and restart the server with rails server but none of that removes the error. I am not using any namespaces.
Question: Why is the method undefined if it's just there?
Note: I found out by chance that the method is found if I name it notifybut maybe that's because I'm overwriting some method from ActionMailer base class, I don't know.
Answer: Restart Sidekiq
I created the mailer class before starting Sidekiq, but I renamed the sample_email method while Sidekiq was already running, so it seems that Sidekiq doesn't recognize new methods on-the-fly.
I renamed the method because I am used to development environment, where you can change almost anything on the fly...
It's because you've defined an instance method, and then you try to call it on a class. Change it to
def self.sample_email
....

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