How to store nil user's goal in a session?
Goals Controller
def create
unless params[:user].present?
# If there is no user, store the goal values to the session
session[:goal_name] = goal_params[:name]
session[:goal_deadline] = goal_params[:deadline]
redirect_to signup_url
else
@goal = current_user.goals.build(goal_params)
if @goal.save
track_activity @goal
redirect_to @goal, notice: 'Goal was successfully created'
else
flash.now[:danger] = 'Required Field: "Enter Goal"'
render 'new'
end
end
User Controller
def create
# create user logic
if @user.save
# Grab the session variable at the same time deleting it
gName = session.delete(:goal_name)
gDeadline = session.delete(:goal_deadline)
# You can make this more complex for error handling
@user.goals.create(name: gName, deadline: gDeadline)
redirect_to success_creation_path
else
...
end
end
How to store data in session from multiple models?
Your code does not check for possible errors when creating Habit & Goal instances, and most likely a simple mistake prevents validation and thus persistence in the Database. ie
@user = User.new(user_params)
if @user.save
...
@user.goals.create(...) # What if it fails ?
@user.habits.create(...) # What if it fails ?
@user.send_activation_email
...
redirect_to root_url
else
render 'new'
end
If the commented lines fail, you will not have any feedback, because you redirect_to another page.
If those lines succeeded (=return false), they will save the Goal/Habit, but if they fail (and return false), well the rest of the process will execute as normal
You should do something like this :
@user = User.new(user_params)
if @user.save
...
unless @user.goals.create(...) # unless = if not
# The warnings variable will be used for user notification
(warnings ||= []) << "Warning ! Goal was not saved because of validation errors"
# Use Rails logger for administrative detection of errors
# => (Rails.logger .warning, .error, .info, .debug) will add to the `rails_root/log/#{environment}` log file, depending on the `:log_level` selected in your configuration
Rails.logger.warn("Warning, auto-creation of Goal for user #{@user.name} on sign_up failed !")
end
unless @user.habits.create(...) # What if it fails ?
(warnings ||= []) << "Warning ! Habit was not saved because of validation errors"
Rails.logger.warn("Warning, auto-creation of Habit for user #{@user.name} on sign_up failed !")
end
@user.send_activation_email
...
flash[:warning] = warnings.join('<br>').html_safe if warnings # Assuming you have some HTML code that renders flash[:warning]
redirect_to root_url
else
render 'new'
end
Of course you could also show why it failed using something like
unless goal= @user.goals.create(...)
(warnings ||= []) << "Warning ! Goal was not saved because of validation errors : #{goal.errors.map{|k,v| "#{k} : #{v}"}.join('<br>')"
end
How to save session with save_with_current_level?
I'm not exactly sure what your problem is, but I think it's again that you're not saving you're levels. Also you have to save your habits first, otherwise ther is no id to assign your levels to
def save_with_current_level
self.save!
5.times {self.levels.create!}
end
How to allow non-user to submit data and save it to new user?
You can save the user with out validation
def create
@user = params[:user] ? User.new(params[:user]) : User.new_guest
if @user.save validate: false
....
end
end
This way the user does not return error when you know the data is just not right
How to Use Sessions to walk user through Sign Up?
That will take some work.
First, fix your routes (post them here as you was rightfully asked).
Next, change your partials into action templates (remove underscore _
from their names).
Then in your controller actions use params[...]
to retrieve data (e.g. params[:session][:name]
for the first step) and save them in session
(session[:name] = params[:session][:name]
).
At the end of action call redirect_to :another_action
(change :another_action to the concrete action you want next – for example in goal
method that would be redirect_to :habit_url
– and you should add that habit
method to your controller).
Then, in your facebook-related methods you will have session[:name]
and other stuff available, so you just fetch it from there and push into database (user.update(name: session[:name])
).
Reading material:
Action Controller Overview – you need to attentively read this first of all
Active Record Basics – how to save user details to database
Rails Routing from the Outside In – just look through that, paying attention to places which are relevant to your current setup
How to skip a session create?
I am going to assume that if they submit the form all the values get populated. That said:
users_controller
def create
@user = User.new(user_params)
make_habit = false
make_goal = false
make_valuation = false
if @user.save
# Goals
name = session.delete(:goal_name)
deadline = session.delete(:goal_deadline)
make_goal = true if !name.blank? && !deadline.blank?
# Values
vname = session.delete(:valuation_name)
vimage = session.delete(:valuation_image)
make_valuation = true if !vname.blank? && !vimage.blank?
# Habits
date_started = session.delete(:habit_date_started)
committed = session.delete(:habit_committed)
trigger = session.delete(:habit_trigger)
action = session.delete(:habit_action)
target = session.delete(:habit_target)
reward = session.delete(:habit_reward)
missed_days = session.delete(:habit_missed_days)
make_habit = true if !date_started.blank? && !committed.blank? #(... continue for whatever else you want to make sure is there)
# Create
@user.habits.create(date_started: Date.parse(date_started), committed: committed, trigger: trigger, action: action, target: target, reward: reward, missed_days: missed_days).create_with_current_level if make_habit
@user.goals.create(name: name, deadline: deadline) if make_goal
@user.valuations.create(name: vname, image: vimage) if make_valuation
@user.send_activation_email
redirect_to root_url
else
render 'new'
end
end
How to delete all of a user's sessions except the current session he's logged in to in RoR?
When a user changes their password, you should also invalidate the current session and issue a new one. This is why many websites ask you to log in again after a password change.
That being said, if you really want to keep the current session that was started using the old password, it should be something like this:
Session.where(user_id: user).where.not(id: current_session.id).delete_all
protobuf-net: how to store in the users session
Note that using protobuf-net here mainly only makes sense if you are looking at moving to a persisted state provider at some point.
Firstly, since you are using in-memory at the moment (so the types are not serialized, AFAIK), some notes on changing session to use any kind of serialization-based provider:
- the types must be serializable by the provider (sounds obvious, but this has particular impact if you have circular graphs, etc)
- because data is serialized, the semantic is different; you get a copy each time, meaning that any changes you make during a request are lost - this is fine as long as you make sure you explicitly re-store the data again, and can avoid some threading issues - double-edged
- the inbuilt state mechanisms typically retrieve session as single operation - which can be a problem if (as you mention) you have some big objects in there; nothing to do with protobuf-net, but I once got called in to investigate a dying server, which turned out to be a multi-MB object in state killing the system, as every request (even those not using that piece of data) caused this huge object to be transported (both directions) over the network
In many ways, I'm actually simply not a fan of the standard session-state model - and this is before I even touch on how it relates to protobuf-net!
protobuf-net is, ultimately, a serialization layer. Another feature of the standard session-state implementation is that because it was originally written with BinaryFormatter
in mind, it assumes that the objects can be deserialized without any extra context. protobuf-net, however, is (just like XmlSerializer
, DataContractSerializer
and JavaScriptSerializer
) not tied to any particular type system - it takes the approach "you tell me what type you want me to populate, I'll worry about the data". This is actually a hugely good thing, as I've seen web-servers killed by BinaryFormatter
when releasing new versions, because somebody had the audacity to touch even slightly one of the types that happened to relate to an object stored in persisted session. BinaryFormatter
does not like that; especially if you (gasp) rename a type, or (shock) make something from a field+property to an automatically-implemented-property. Hint: these are the kinds of problems that google designed protobuf to avoid.
However! That does mean that it isn't hugely convenient to use with the standard session-state model. I have implemented systems to encode the type name into the stream before (for example, I wrote an enyim/memcached transcoder for protobuf-net), but... it isn't pretty. IMO, the better way to do this is to transfer the burden of knowing what the data is to the caller. I mean, really... the caller should know what type of data they are expecting in any given key, right?
One way to do this is to store a byte[]
. Pretty much any state implementation can handle a BLOB. If it can't handle that, just use Convert.ToBase64String
/ Convert.FromBase64String
to store a string
- any implementation not handling string
needs shooting! To use with a stream, you could do something like (pseudo-code here):
public static T GetFromState<T>(string key) {
byte[] blob = {standard state provider get by key}
using(var ms = new MemoryStream(blob)) {
return Serializer.Deserialize<T>(ms);
}
}
(and similar for adding)
Note that protobuf-net is not the same as BinaryFormatter
- they have different expectations of what is reasonable, for example by default protobuf-net expects to know in advance what the data looks like (i.e. public object Value {get;set;}
would be a pain), and doesn't handle circular graphs (although there are provisions in place to support both of these scenarios). As a general rule of thumb: if you can serialize your data with something like XmlSerializer
or DataContractSerializer
it will serialize easily with protobuf-net; protobuf-net supports additional scenarios too, but doesn't make an open guarantee to serialize every arbitrary data model. Thinking in terms of DTOs will make life easier. In most cases this isn't a problem at all, since most people have reasonable data. Some people do not have reasonable data, and I just want to set expectation appropriately!
Personally, though, as I say - especially when large objects can get involved, I'm simply not a fan of the inbuilt session-state pattern. What I might suggest instead is using a separate per-key data store (meaning: one record per user per key, rather than just one record per user) - maybe just for the larger objects, maybe for everything. This could be SQL Server, or something like redis/memcached. This is obviously a bit of a pain if you are using 3rd-party controls (webforms etc) that expect to use session-state, but if you are using state manually in your code, is pretty simple to implement. FWIW, BookSleeve coupled to redis works well for things like this, and provides decent access to byte[]
based storage. From a byte[]
you can deserialize the object as shown above.
Anyway - I'm going to stop there, in case I'm going too far off-topic; feel free to ping back with any questions, but executive summary:
- protobuf-net can stop a lot of the versioning issues you might see with
BinaryFormatter
- but it isn't necessarily a direct 1:1 swap, since protobuf-net doesn't encode "type" information (which the inbuilt session mechanism expects)
- it can be made to work, most commonly with
byte[]
- but if you are storing large objects, you may have other issues (unrelated to protobuf-net) related to the way session-state wants to work
- for larger objects in particular, I recommend using your own mechanism (i.e. not session-state); the key-value-store systems (redis, memcached, AppFabric cache) work well for this
Why did you need to save something in gorilla session?
The store.Get method uses the request object to get the sessionid, then gets the session data from the store, and then creates the session object.
session.Values uses a map to save the session data. Reading and writing session.Values is to manipulate the session data. Finally, the session.Save method is called to save the data from the new serialization to the store.
How to store email in session and save upon create to new user?
One option would be to set a cookie when they submit the challenges form and then check for the existence of that cookie:
unless cookies[:challenges_email].present?
cookies[:challenges_email] = {
value: params[:email],
expires: 1.year.from_now
}
end
Then on the page where the user signs up, check for the existence of the cookie (inside user create
action):
if cookies[:challenges_email].present?
@user.email = cookies[:challenges_email]
end
If you want to do this for additional fields you would set an additional cookie as they are meant to be key-value pairs.
Related Topics
Ruby Variable Name with Double Underscores
Activerecord Connection Warning. (Database Connections Will Not Be Closed Automatically)
How to Remove Non-Printable/Invisible Characters in Ruby
Couldn't Find User with Id=Sign_Out
Delete Contents of Array Based on a Set of Indexes
Ruby on Rails Incompatible Library
Changing Songs on Jplayer by Clicking a Link, Hosted on Amazon S3
Storing Passwords for External APIs - Best Practice
Inherit Class-Level Instance Variables in Ruby
How to Extract a Single Character (As a String) from a Larger String in Ruby
How to Print Stdout Immediately
Handling Iframe with Capybara Ruby
How to Read Only X Number of Bytes of the Body Using Net::Http
Rspec: Should Be (This or That)
How to Enable Chromedriver Logging in Ruby Capybara with Selenium