Show Menu

Elixir Cheat Sheet by

elixir programming language gotchas
elixir

Errors

undefined function xxx_path/3
add the resource to the router ( resources "­/xx­x", xxxCon­tro­ller)
cannot use ^xxx outside of match clauses
add import Ecto.Query to the controler

hex.pm gotchas

password resets: when you forget a password and reset it, you have an option to revoke api keys, which is default, you must regenerate an api key via cli: mix hex.user auth

errors: insert­ed_at: can only modify a release up to one hour after creation
if you get this error it is because you didnt update the current version within 1 hour, so you need to bump the version located in mix.exs

phoenix gotchas

to render a different view/t­emplate combo for a controller
conn
|> put_vi­ew(­xxx­Web.xx­xPa­rti­als­View)
|> render­("xx­x.h­tml­", assigns: assigns)

to not render templates for only certain controller actions
plug :put_l­ayout, false when action in [:xxx, :yyy ]

to render certain layout for only certain controller actions
:put_l­ayout, {xxxWe­b.x­xxView, :layout} when action in [:xxx, :yyy]

in order to use https, the config goes into xxxWeb­/co­nfi­g/x­xx.exs
 

Elixir Gotchas

to access a config
var config = Applic­­at­i­o­n.g­­et­­_e­n­v­(:my, :title)
 

ecto gotchas

cast_a­ssoc/3 is only used in the changesets of schemas that have has_one or has_many. It invokes the cast/3 validation in the changeset of the associated schema.

assoc_­con­str­aint/3 is used in the changesets of schemas that have belong­s_to. It checks to make sure the parent schema is in the database so an orphaned record isn't inserted.

foreig­n_k­ey_­con­str­aint/3 is used similarly to assoc_­con­str­aint/3, except that it uses the database to validate the foreign key constr­aint, whereas assoc_­con­str­aint/3 uses the Ecto schema defini­tion.

Brunch Gotchas

bundling styles­heets:
in phoenix 1.3 to bundle a custom directory it has to be both added to watched as well as the jointo under styles­heets the parent dir is app/assets and is implied
in this example we are bundling xxxweb­/as­set­s/b­und­led­/pa­ge/­*.css to xxxweb­/pr­iv/­sta­tic­/cs­s/p­age­/ve­ndo­r.css
ex: for css
joinTo: {"cs­s/p­age­/ve­ndo­r.c­ss": /^bund­led­\/(­?:c­ss­\/)?­page/},
watched: ["st­ati­c", "­css­", "­js", "­ven­dor­"­,"bu­ndl­ed"],

for the js i typically separate what i want bundled into a bundled dir, we also need to include node_m­odules we do so like this.
ex for js.
"­js/­use­r/v­end­or.j­s": /^(bun­dle­d\/­js­\/us­er|­ven­dor­|de­ps|­nod­e_m­odu­les­).*/,

but we also need to join the main file respon­sible for importing the channels etc typically called app.js we do so by adding the path to this file to the module­s:a­uto­require it is important to remember the path is pre compia­tion, (this took days to understand this) compiling like this.
ex.

modules: {
autoRe­quire: {
"­js/­use­r/v­end­or.j­s": ["bu­ndl­ed/­js/­use­r/a­pp"],
}

},

Download the Elixir Cheat Sheet

1 Page
//media.cheatography.com/storage/thumb/mithereal_elixir.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.