Saving a JSON value in relation to current user - Rails - ruby-on-rails

I need to accept Bitcoin and I am using Block.io API gem. In my payments controller I have a method that generates a new Bitcoin address from the API for the user to transfer bitcoins to. I need to know how to save this Bitcoin address in relation to the current user. I do have a User model and current_user is an instance of user.
The controller looks like this:
class PaymentsController < ApplicationController
def index
#new_address = BlockIo.get_new_address
end
end
In the view when the user visits the Payments index page to make a payment, they get a new generated Bitcoin address.
In the view I access the JSON output to show the user the address like this:
<%= #address["data"]["address"] %>
And the JSON data that I receive looks like this:
{"status"=>"success", "data"=>{"network"=>"BTCTEST", "address"=>"2MstFNxtnp3pLLuXUK4Gra5dMcaz132d4dt", "available_balance"=>"0.01000000", "pending_received_balance"=>"0.00000000"}}
How can I save the bitcoin address from JSON to the database, specifically in relation to the current_user?
Any comment or answer will be greatly appreciated.

This might help you, give it a shot: http://rubyjunky.com/rails-activerecord-serialize.html

Related

Ruby on Rails - Setting different names for url

I am completely new to Rails and I have a database that links to a certain page depending on the user's search but it will always give me the id.
For example if a user searches, I will get, "localhost:3000/fruit/1" instead of "localhost:3000/fruit/apple". Does anyone know how to switch the url from an id to name?
You need to define a 'to_param' method in the model you are generating a link for, e.g.
class Fruit < ActiveRecord:Base
def to_param
name
end
end
Then in your controller you need to change the find for the show action to find by the attribute you are using in your 'to_param' method, e.g.
#fruit = Fruit.find_by(name: params[:id])
See http://api.rubyonrails.org/classes/ActiveRecord/Integration.html#method-i-to_param for additional details

Rails 4 - Finding related user

Task: Showing the profile of an employee straight away after his login.
Issue:
class WelcomeController < ApplicationController
def index
#employee = Employee.find_by_email(params[#current_user.email])
end
end
I tried to code in many ways to associate the email of the current user with his respective details from the employees table and the farthest that I could get was it:
I am sure that I am writing something wrong in this line inside the index thing, but I am researching and all things that I found and tried did not get the employee related to the current user.
Try with this code:
class WelcomeController < ApplicationController
def index
#employee = Employee.where(email: current_user.email).first
end
end
When using Devise, the current user is an instance variable, so you don't need to prefix it with #.
If you are going to have a lot of users, is a good practice to create an index in your database for the email column.
I like kjmagic13's answer. Use the
#current_user.id
It pulls all the info associated with the user from the database
Mori's answer is also good.
The following SQL line in your logs corresponds to the Employee.find_by_email call:
SELECT "employees".* FROM "employees" WHERE "employees"."email" IS NULL LIMIT 1
As Mori pointed out, this means you're finding the employee with a nil email, which means that params[#current_user.email] is nil. Since you have no parameters, there's no need to refer to the params hash regardless. You should refer just to the #current_user.email:
Employee.find_by_email #current_user.email
As Mori's answer states, you probably didn't intent do use #current_user.email as a hash key into params. I think you're trying to look up the employee record for the current user by email (not by an email submitted as a parameter), like so (also avoiding deprecated find_by_* helpers):
#employee = Employee.find_by(email: #current_user.email)
I don't think you want to try to do Employee.find(#current_user.id) - that's just going to look up the Employee whose id matches the current_user's id - unless Employee and User use the same table that's not going to be meaningful
Why not just find by the ID? find_by_* are old.
#employee = Employee.find(#current_user.id)

How do I use a Twitter username as my user id?

I'm running a rails 4 app using Omniauth with Twitter. I'm trying to achieve something close to producthunt.com, where they authenticate users and use their Twitter username as their url id.
From what I understand, you want the url to look like this: example.com/users/username
instead of example.com/users/123
If so, all you have to do is change the way you find the de correct user in your Users (or whatever you call your user model) controller. Currently it probably looks like this:
#Users Controller
def show
#user = User.find(params[:id])
end
# Your path to that user is probably this:
user_path(123) #basically you pass the user.id
The code above is using Model.find(#) to look for the user. The .find() looks the user up by its id#. Instead, you want to find it by the username, not id. To do this use the Model.find_by You can see all the ways of querying here.
Also, whenever you look for the path to find the user show page, istead of sending the id # to the url, you now have to send the username string.
Your new setup should look like this:
#Users Controller
def show
#user = User.find_by :username params[:id]
#this assumes you have it in your DB as username. Some twitter apps save it as screen_name.
end
# Your path to that user is probably this:
user_path('username') #basically you pass the username instead. current_user.username? I dont know what you call in in your app.
Hope that helps. Let me know if you have questions.

List all the devise profiles

I have installed devise and I can create users. I am trying to display all of the users so that people can see who has signed up. I would eventually like to allow people to send messages etc..(think Tinder).
Any guidance on displaying profiles to the user?
This is big question to answer here and explain .I think you want to display listing of all the users and for every user there would be a profile page(show ). what you need to do is create controller having same name as devise name.I can give you small idea about that.
Class UsersController < ApplicationController
def index
#users = User.all
end
def show
#user = User.find(params[:id]) //you can change it according to rails4 standards
end
....
....
....
end
with this you can display all user and display there profiles.
Now for messaging you need to create a different model Message and which will have fields sender,receiver and msg .both sender and receiver will be user_id ,sender you can get from session and receiver you can get from profile itself.and remember user can have many messages
But for making this real time you have to use web sockets which will be different question to answer.

How to record a user's IP address when they sign up in Ruby on Rails?

I have a User model with an "ip" attribute. I want to save the user's IP address when they sign up in the User model.
The only problem is that it seems like when I make a method in the User model:
def set_ip
self.ip = request.remote_ip
end
I get an error message saying "request" doesn't exist, so it doesn't look like it exists in the model.
Is there any way to set an IP address in the model or do I have to do this in the controller?
request is a controller method so you can't call it from inside a model. Why not add the IP to params before you create your user? Something like this:
params[:user][:ip] = request.ip
#user = User.create(params[:user])

Resources