Ruby on Rails session: My own best practice !!!

Okay here’s my first post on non-human language topic, so bear with me 😀

So I’ve got some difficulties dealing with rails session a while ago…
after looking for solution on the net, and found nothing that’s really clear and meets my requirement,
in the end I ended up on the rails api and rails guide

But then again I have to admit I’m too lazy to read them, so i finally work my own shit up :))

here’s what i do :
i need the session to be available in the whole application, in order to do that you have to make a private method in the application_controller.rb file
for example, in my case i want an invoice session for a cart app
here’s what the content of the file might look like:

class ApplicationController < ActionController::Base
    helper_method :invoice_session

  def invoice_session
    @invoice_session ||= session[:invoice_id]

well I think that’s it, after that you can set the session storage method on any controller for example
the cart_controller.rb index method that might look like this

class CartController < ApplicationController
  def index
      #Here's the session storing being placed
      #session[:session_name] = method (eg. Model.find[:blabla].field
      #below is an example
      session[:invoice_session] = Invoice.find[:first].invoice

Now to retrieve the session is very simple, you can include the method earlier in the application_controller.rb which is the invoice_session in any controller or view

well that’s all folks, this is how I did it
hope you can find it usefull



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s