0
  def create
    chef = Chef.find_by(email: params[:email])
    if chef && chef.authenticate(params[:password])
      **session[:chef_id] = chef.id**
      flash[:success] = "You logged In"
      redirect_to recipes_path
    else
      flash.now[:danger] = "Check your email or password"
      render 'new'
    end
  end

What does

session[:chef_id] = chef.id

do? Is that session[:chef_id] a kind of variable or something? To which the id of a chef is assigned? Can I use some other name there?

dave
  • 11,641
  • 5
  • 47
  • 65
Vijay
  • 53
  • 1
  • 1
  • 9

2 Answers2

0

session[:chef_id] = :foo stores chef_id key in session with value of :foo.
So you can fetch assigned :foo value later by calling session[:chef_id].

In your code it is assigned to id of chef who has email equals to params[:email].
You can name :chef_id whatever your like but I think it is pretty normal name.

Aleksey
  • 2,289
  • 17
  • 27
0

That's storing the chef_id in the session, which is a way of persisting data accross multiple requests. It's not specific to Rails, as the session is also available in all web applications. You could read more on the Rails session here and more on web sessions here

Community
  • 1
  • 1
oreoluwa
  • 5,553
  • 2
  • 20
  • 27
  • So @oreouluwa is that a part of convention? Can I use session[:chef] = chef.id Will that work? – Vijay Sep 25 '16 at 12:09
  • yes, the key doesn't really matter, as long as it doesn't affect other parts of your application. For example, somewhere you may be doing `Chef.find(session[:chef_id])`, in which case you have to change to `Chef.find(session[:chef])`. However, for clarity, since it's storing the `chef_id` not the `chef` object itself, I'd advise that you leave it as `chef_id` – oreoluwa Sep 25 '16 at 12:14
  • Yea got it ! Thank you ! – Vijay Sep 25 '16 at 12:15