Difference between session and parameters in controller class

I am looking at an example of rails for shopping carts and in the ApplicationController class I see code like this:

class ApplicationController < ActionController::Base
  protect_from_forgery

  private

    def current_cart 
      Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound
      cart = Cart.create
      session[:cart_id] = cart.id
      cart
    end
end

      

so it uses Cart.find (session [: cart_id])

Then I go to carts_controller.rb and CartController category and I see code like this:

 def update
    @cart = Cart.find(params[:id])

    respond_to do |format|

      

so Cart.find (params [: id]) is used here

But I can't figure out why we were using session to pass parameters to AppController, but we were using normal parameters in CartController and could we have used them using swithc? or is this how rails work and always the session goes to the AppController? It would be greta if someone can explain this in more detail

+3


source to share


1 answer


params

live in the url or in the body of the form post, so it disappears as soon as the request is made.

The session persists across multiple requests (information is often stored in cookies, but this depends on your configuration).



In short:

  • params: only one request (create one object, access one specific page)
  • session: information is saved (cart, logged in user ..)
+8


source







All Articles