feviskus

feviskus

[Programming Phoenix] How do sessions work in Programming Phoenix?

Hello there,

I`m currently reading Programming Phoenix and struggle to understand some parts of the code.
2 Examples:

[web/models/user.ex]
def changeset(model, params \\ :empty) do
  model
  |> cast(params, ~w(name username), []) # WTF is ~w? Never explained
  |> validate_length(:username, @username_validation_length)
end

Why the ~w/1 function and this strange name? What does name/1 do?
When I look up the docs for cast/3 , I can just replace ~w/1 with [:name, :username] but I wan’t to know what’s the difference (besides eye cancer).

Another problem is the following code:

[web/controllers/auth.ex]
def call(conn, repo) do
  user_id = get_session(conn, :user_id)
  user = user_id && repo.get(Rumbl.User, user_id) # && should return boolean
  assign(conn, :current_user, user)
end

The docs are not really helpful in describing get_session/2:

get_session(conn, key)
Returns session value for the given key

I can assume that get_session/2 returns an int (but not because of the docs..).
I dont know what’s going on on the other lines of code, he is using this session id to get a user from the repo, but I thought the session id would be some kind of random value…
After that, I dont know whats going on with user (an int) and the returned struct…He is comparing them somehow…how is this working?
Thanks for taking time and reading this, I hope you can help me.

First Post!

OvermindDL1

OvermindDL1

~w is a sigil, it calls the macro named sigil_w at compile-time, which takes what is between the (/) pairs and parses it out via space separation into atoms in a list. It is explained at: https://elixir-lang.org/getting-started/sigils.html :slight_smile:

Basically ~w(name username) === [:name, :username]

No, I don’t get the point either, I just use atoms straight. I personally think it is a perfectly good wasted sigil name (we only have a limited amount after all!). ^.^;

Last Post!

NobbZ

NobbZ

As I said, its often used by the ruby folks, and please remember where José comes from :slight_smile:

Also I think it might come in handy when you have a lot of words which contain quotes or are IPs/hosts:

~w[foo"bar bar'baz ::1 example.com]a

If you have a lot of them and try it “traditionally” you’ll have some overhead I’d guess. But in simple cases as casts arguments I prefer explicit list of atoms as well.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement