The layout and details
-
Routes
- /
- root of the app will eventually be an aggregate of incoming from Micro.Blog + our own feed (maybe)
- /profile
- 'Settings'
- this is where we'll be able to add targets for our re-posting feature 'echo'
- add token keys and incorporate 3rd party API's
- /profile/posts
- list of current users posts
- /posting
- this is the 'editor' with fields for title and content
- Successful 'post' returns the user to an updated list of posts
Authorization
- now that we have authentication out of the way. Let's deal with only that user can post to that users account.
Just do it already!
MB:postit> mix phx.gen.html Posting Post posts title:string content:text * creating lib/postit_web/controllers/post_controller.ex * creating lib/postit_web/templates/post/edit.html.eex * creating lib/postit_web/templates/post/form.html.eex * creating lib/postit_web/templates/post/index.html.eex * creating lib/postit_web/templates/post/new.html.eex * creating lib/postit_web/templates/post/show.html.eex * creating lib/postit_web/views/post_view.ex * creating test/postit_web/controllers/post_controller_test.exs * creating lib/postit/posting/post.ex * creating priv/repo/migrations/20190717045929_create_posts.exs * creating lib/postit/posting.ex * injecting lib/postit/posting.ex * creating test/postit/posting_test.exs * injecting test/postit/posting_test.exs Add the resource to your browser scope in lib/postit_web/router.ex: resources "/posts", PostController remember to update your repository by running migrations: $ mix ecto.migrate MB:postit>
That didn't take long. Our first error. 😭
assign @current_user not available in eex template.
def controller do quote do use Phoenix.Controller, namespace: PostitWeb import Plug.Conn import PostitWeb.Gettext alias PostitWeb.Router.Helpers, as: Routes end end
Plugs:
what did i learn [dot] info - plugs feel conceptually a lot like express middleware, where it takes the connection or request and does a function on it, then returns the conn / request for the next plug or middleware
-
There are two types of plugs in the Phoenix. They are module plugs and function plugs. We are going to cover both of them in this article.
- module has init and call
- HAHAH we had a function plug, I just had no idea that is what it was or called
- Where do we want the plug triggered?
-
having stumbled into it in an effort to 'DRY' out our code. And really, there will probably be more controllers in our app over time and I feel like I have really hit the sweet spot of learning
- question - how do i reuse a function in multiple controllers
- start with a helper
- add it to the web.ex for controller that is called as a macro for every controller to import the helper
- use that helper function on a plug for the controller,
- just make it a plug function or module
Alias vs Import
Terms
@doc false def changeset(post, attrs) do post |> cast(attrs, [:title, :content]) |> validate_required([:title, :content]) end
-
conn
-
defp
- Inside a module, we can define functions with def/2 and private functions with defp/2. A function defined with def/2 can be invoked from other modules while a private function can only be invoked locally.
Picks Resources
Elixir Casts Pheonix Contexts
Alchemist Camp
Elixir Phoenix Realworld
Pheonix Conn CheatSheet
indieweb
Micro [dot] Blog
Follow
-
JavaScript to Elm
-
Jesse Tomchak