Gotta admit, once I got the auth working and the deployment going, I was a bit hesitant to start adding to the project again. Because that meant, failed builds, unknown errors, and a general sense of failing. We just got things working! But's the sayin' "if it's not broken, you're not working hard enough"? So back at it I guess.
The layout and details
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.
we’d forgotten to add the security plug to get for auth and the conditional in our Navbar is looking for
current_user in order to render the login or logout buttons. happy that was simple.
web.exfile has this cool macro
def controller do quote do use Phoenix.Controller, namespace: PostitWeb import Plug.Conn import PostitWeb.Gettext alias PostitWeb.Router.Helpers, as: Routes end end
Create a controller helper and add it in here!
plugsfeel 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.
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
web.exfor controller that is called as a macro for every controller to import the helper
What is the difference between
alias use shorter name instead of the fully qualified name or prefix
Keyword, etc) which is converted to an atom during compilation. For instance, the
Stringalias translates by default to the atom
requirerequire this module in order to use it’s macros
importimport functions from module so you don’t have to use the prefix
useinvokes the custom code defined in that module as an extension point
@doc false def changeset(post, attrs) do post |> cast(attrs, [:title, :content]) |> validate_required([:title, :content]) end
def/2and private functions with
defp/2. A function defined with
def/2can be invoked from other modules while a private function can only be invoked locally.