Rails 4.0 User Authentication/Signup - ruby-on-rails

I'm currently doing this tutorial:
http://net.tutsplus.com/tutorials/building-ribbit-in-rails/
but tried to update it to 4.0 myself since it seems a bit outdated.
https://github.com/erosenberg/myribbit
One of the things that stands out to me is that the tutorial was using 'attr_accessible' whereas I was using a private method to define the params for users to signup.
The solution doesn't have to match the tutorial (I only just got to the part with Sessions) but I would love to be able to see how to fix this specific error that isn't making sense to me and constantly sending me down a rabbit hole:
ArgumentError in Users#new. wrong number of arguments (1 for 0).
app/controllers/application_controller.rb:7:in `current_user'
Any help is greatly appreciated. Thanks!
EDIT: Here is a screenshot of my error in case nobody else is seeing it:

You have a typo in your code: https://github.com/erosenberg/myribbit/blob/master/app/controllers/application_controller.rb#L7
session [:user_id] should be session[:user_id].

Related

devise-two-factor how to get code valid for custom time to send over SMS/Email etc

I am trying to implement 2FA(two factor authentication) in my existing rails 4.2.10 application, I have configured many bits.
Issue I am facing is to get/retrieve a code which is valid for 5 minutes and send this code over to user on his defined phone number or email.
I did tried ROTP::TOTP.new(user.otp_secret).at(Time.now), guessing from gem's source code, which seems to work fine and give a valid otp_code in console, but in sessions_controler, as weird as it sounds, user.otp_secret is null, always...
I have posted an issue on the gem.
I don't think this can be bug, rather this is a functionality I want to build.
My stack:
Ruby: 2.4.2
Rails: 4.2.10
Devise: 4
attr_encrypted: 1.4(if it matters)
Additionally, I want to extend drift period(code acceptance time) to 5 minutes. I think that will be easy, but doing it for single code, not universally, or for all codes, this has me thinking for a while now.
My main issue is the first one, getting the code to send through SMS, this is a subproblem, which I think is doable, but if anyone has/had experience with this and can help, that will be great.
UPDATE: I updated attr_encrypted and restarted the system, it started working, also I realized there is a method current_otp in which devise_two_factor adds in the user model, so I started using that. BUT after a few minutes, it is also throwing the same issue of user.otp_secret being nil. Its getting weird...
UPDATE 2/Hacky solution: Weirdly enough, I had to add these 3 methods in user model and everything started working:
def encrypted_otp_secret
self[:encrypted_otp_secret]
end
def encrypted_otp_secret_iv
self[:encrypted_otp_secret_iv]
end
def encrypted_otp_secret_salt
self[:encrypted_otp_secret_salt]
end
As you can suspect, i got here by examining a behavior thatdoing user.encrypted_otp_secret was giving me nil while it was not, even after reloading user model. And doing user[:encrypted_otp_secret] was giving me the actual value.
It seems like a bug in attr_encrypted. I am not sure yet.
For anyone else that runs into this issue, I have found a next step needed to get the current_otp method to work. In the method pre_otp method call
> u = User.find_by(email: 'test#example.com')
> u.otp_required_for_login = true
> u.otp_secret = User.generate_otp_secret
> u.save!
and then you can call u.current_otp...
https://blog.tommyku.com/blog/integrating-two-step-two-factor-authentication-into-rails-4-project-with-devise/

What is the difference between find and find_by?

I know from the rails documentation that find will result in a RecordNotFound error. However, the find_by method simply returns nil if a record is not found. Returning nil seems more intuitive to me and safe in general, so I am wondering if there is any benefit in using the find method over the find_by method?
For example, what is the difference between the following:
myRecord.find(1)
and
myRecord.find_by(id: 1)
If the only difference is that find raises an error when a record isn't found, I don't really see the benefit in using find.
EDIT
For all the people that jumped on my question and said that it had been already answered, you are wrong. I clearly stated that I knew find returns an error when a record is not found (which is what everyone else emphasizes in their answers) and that find_by returns nil. I want to know if there are any other differences.
In your specific example there is little difference between the two of them other than the error vs nil which you mention. Whether you want to handle an error or nil is totally up to you.
For a great explanation of understanding when an error is preferable to nil and vice versa read this.
If you are searching by an attribute other than id using find will not work as it can only access elements by their id.

NoMethodError undefined method `encoding' for nil:NilClass CGI.escape

came across this problem a little earlier today, anyone know what might be going on? I'm a rookie with api's. Tried Googling but not much luck so far..
Error:
NoMethodError in ArtistsController#index
undefined method `encoding' for nil:NilClass
the problem line is:
response = HTTParty.get("https://api.spotify.com/v1/search?q=#{CGI.escape params[:query]}&type=artist")
which makes me think it might be an issue with CGI.escape.
If I enter the following url with a query already at the end everything works fine:
http://localhost:3000/search?utf8=%E2%9C%93&query=Caribou&commit=Search
However, if enter the following url without a query I get the error mentioned above:
http://localhost:3000/search
Guess that explains the nil part, but I don't no how to get past this..
Think that's all the info I need to give for this issue but let me know if more is needed.
If params[:query] isn't defined, then CGI.escape is receiving a nil argument.
Make sure params[:query] is defined if you're referencing it, and/or don't call CGI.escape when searching by type only, and not keyword. (Assuming that's possible--that's what the empty search above seems to imply.)

wrong number of arguments (2 for 1) - When i try to add data in rails admin

I am getting wrong number of arguments (2 for 1) in rails admin when i tried to add data's from backend , I have attached screenshot, Please do see that and help me out!
Please visit https://imageshack.com/i/n7dz4up
I know this is going to blow your mind, so have a seat, then read on.
You passed two arguments to a method that only expected to get one.
If you need more help be sure to come back with more details, like the code and what you've done to fix the problem so far!

Rails: Production Authlogic Problem with find_using_perishable_token

So I'm pretty puzzled by this one. This works in development mode and had worked in production mode before, but I just got this hoptoad error that said this:
An error has just occurred. View full details at:
http://goldhat.hoptoadapp.com/errors/2418099
Error Message:
NoMethodError: undefined method `reset_perishable_token!' for #
Where:
password_resets#create
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb, line 260
Now my password resets and user email verification doesn't work because this method (and the reset_perishable_token! method) is apparently undefined. I can't conceive of where to begin with this problem).
Might make sure you have the perishable_token in your database, if you don't I don't think authlogic defines the method.
I am facing the same issue "NoMethodError: undefined method `reset_perishable_token!'" and it take me 8 hours to figure out. I hope this can help someone in the future.
1) Make sure you have "perishable_token" column
2) Make sure you get the correct user record and call "reset_perishable_token" method
My problem is i get the wrong user record to call the "reset_perishable_token" method.
Before: user = User.where(["users.email = ?", email])
After : user = User.where(["users.email = ?", email]).first

Resources