NoMethodError User Authentication - ruby-on-rails

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.

Related

getting undefined method `include?' for nil:NilClass

I'm trying to follow a tutorial to create a messaging app but while creating a conversation controller I faced an error. My goal is to check whether a conversation between the current_user and the selected user exists or not. If it doesn't exist then I want to create a new one.
but when I tried this I got the error from the bellow. It would be a big help.
The error log
NoMethodError (undefined method `include?' for nil:NilClass):
app/controllers/conversations_controller.rb:30:in `conversated?'
My controller from Conversations
def create
#conversation = Conversation.get(current_user.id, params[:user_id])
add_to_conversations unless conversated?
end
private
def conversated?
session[:conversations].include?(#conversation.id)
end
Let me know if you need any other parts added to the question. Thanks in advance.
When you are first calling session[:conversations], the value seems to be nil and such include? is not a method that nil knows how to respond to. I think you can make a small tweak in that check like so:
def conversated?
session[:conversations]&.include?(#conversation.id)
end
The & is a safe navigation operator where the method is only called if a value exists.

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
....

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

Custom id column issue

in my listing model i setup a method
def set_listing_number
listing_number="TM#{created_at.year}#{id}"
end
i have a couple of records that was created before i ran the migration to create the listing_number column. i wanted to update all the records at once but keep receiving this error.
here's the code that i ran that produce the error in console verifying if the method . it works i cannot save the assignment.
listing_number=listing.set_listing_number
=> "TM2014574"
2.0.0-p247 :003 > listing_number.save
NoMethodError: undefined method `save' for "TM2014574":String
i tried a couple with no success , that i also found here like this one
Listing.all.each{|n| n.update_attributes(:listing_number =>"TM#{n.created_at.year})}
the question how do i update the previous record at once. probably it's pretty basic but can't figure it out. thanks
# This method in Listing Model
def set_listing_number
listing_number="TM#{created_at.year}#{id}"
end
# In controller
Listing.all.each do |listing|
listing.set_listing_number
listing.save
end
or you can refactor this as
Listing.all.each do |listing|
listing.update listing_number: "TM#{listing.created_at.year}"
end
You're calling save on a string. You need listing.save in your code.

Resources