Scaffolding with Phoenix
2019-04-27
I love scaffolding, probably more than I should! It gets me playing with data right away and lets me put up a skeleton quickly. The nice thing about scaffolding with Phoenix (compared to Rails) is that less “cruft” is created. No extra css, coffee script, or fixtures created. Yay!
The special command for scaffolding is:
mix phx.gen.html Accounts User users name:string age:integerin order:
- 
mixis the sweet tool for running all your tasks
- 
phx.gen.htmlis the namespaced module containing the generation code
- 
Accountsis your context, the “group” or “collection” it belongs to.
- 
Useris the name of the module that contains your schema and changeset
- 
usersis the name of your database table
- and the remainder are your attributes.
The following attributes are avaliable:
- 
:integer
- 
:float
- 
:decimal
- 
:boolean
- 
:map
- 
:string
- 
:array
- 
:references
- 
:text
- 
:date
- 
:time
- 
:time_usec
- 
:naive_datetime
- 
:naive_datetime_usec
- 
:utc_datetime
- 
:utc_datetime_usec
- 
:uuid
- 
:binary
- 
:datetime- An alias for:naive_datetime
I find the references option to be interesting, used like:
user_id:references:usersOh? You just want to generate the context and not the html? Then use this command:
mix phx.gen.schema Accounts.User users name:string age:integer
    Like what you see?
  
Related Posts
2019-04-27
      
      
      
2019-04-16
      
      
      
2019-04-26
      
      
      
2019-04-26
      
      
      
2019-08-03