I have read that it is possible to store sessions in rails without user authentication (referenced this), however I am not sure how to go about it in my user model. I am basically creating a survey and only want to store the user's IP address and ID (I cannot have any other identifiable information for IRB purposes). The user will be referenced by another model, and I want responses of the survey to be owned by the current user. Tips?
Asked
Active
Viewed 209 times
1
-
Where does the ID come from? – Ruslan Mar 02 '17 at 20:05
-
@Ruslan I am not sure. That is part of my problem. I basically want to create a user without traditional credentials, so I am not sure how the ID would be generated. – knod Mar 02 '17 at 20:13
-
It's generally better to post actual code snippets so we have more concrete examples of your problem to help solve. Asking a fairly general question like this makes it hard to answer, and thus unhelpful to the site. Please post some code snippets and specific questions you would want help with, thanks! – thaavik Mar 02 '17 at 21:40
2 Answers
0
You could use securerandom
to generate the unique ID's for users
require 'securerandom'
SecureRandom.uuid
Then you can store that in the session to keep track of the user
def survey_user
session[:survey_user] ||= SecureRandom.uuid
SurveyUser.find_or_create_by(id: SecureRandom.uuid)
end
So now when a user comes to do your survey, you have a model for them in database, and can create associations with their answers

Ruslan
- 1,919
- 21
- 42
-
1Thank you so much! I hadn't seen SecureRandom before, and this answer led me to the new has_secure_token in Rails 5. – knod Mar 02 '17 at 21:01
-
0
You don't really need to create a session. It will be created automatically when needed, i.e. when you store something in it. You should just need something like:
session[:id] = ...
session[:ip_address] = ...
(Although there should be no need to store the ip address at all since it should always be available in the request
object)

Marc Rohloff
- 1,332
- 7
- 8