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
~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 ![]()
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
As I said, its often used by the ruby folks, and please remember where José comes from ![]()
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









