joy-framework / joy

A full stack web framework written in janet
https://joy.swlkr.com
MIT License
535 stars 30 forks source link

Error following tutorial. Unknown symbol "id" on line 28 #34

Closed ryanford closed 4 years ago

ryanford commented 4 years ago
compile error: unknown symbol "id" on line 28, column 23 while compiling src/routes/account.janet
compile error: unknown symbol "account/index" on line 8, column 3 while compiling src/routes.janet
compile error: unknown symbol "routes/app" on line 6, column 14 while compiling src/app.janet
compile error: unknown symbol "app" on line 4, column 1 while compiling main.janet

Following https://github.com/joy-framework/joy/blob/master/docs/tutorial.md . After creating db, running migration and creating route, server fails to start.

swlkr commented 4 years ago

I also followed the tutorial and I couldn't reproduce this 😔 here is what the src/routes/account index function looks like after I generated it:

(defn index [request]
  (let [{:db db} request
        account (fetch-all db [:account])]
   [:div
    [:a {:href (url-for :account/new)} "New account"]
    [:table
     [:thead
      [:tr
       [:th "id"]
       [:th "email"]
       [:th "password"]
       [:th "created-at"]
       [:th "updated-at"]
       [:th]
       [:th]
       [:th]]]
     [:tbody
      (map
       (fn [{:id id :email email :password password :created-at created-at :updated-at updated-at}]
         [:tr
          [:td id]
          [:td email]
          [:td password]
          [:td created-at]
          [:td updated-at]
          [:td
           [:a {:href (url-for :account/show {:id id})} "Show"]]
          [:td
           [:a {:href (url-for :account/edit {:id id})} "Edit"]]
          [:td
           (form-for [request :account/destroy {:id id}]
            (submit "Delete"))]])
       account)]]]))
swlkr commented 4 years ago

I should mention I'm using janet 1.6.0 and joy 0.5.2

ryanford commented 4 years ago

this is what you should be getting i think:

(import joy :prefix "")

(defn account [request]
  (let [db (get request :db)
        id (get-in request [:params :id])]
    (fetch db [:account id])))

(defn index [request]
  (let [{:db db} request
        account (fetch-all db [:account])]
   [:div
    [:a {:href (url-for :account/new)} "New account"]
    [:table
     [:thead
      [:tr

       [:th]
       [:th]
       [:th]]]
     [:tbody
      (map
       (fn [{}]
         [:tr

          [:td
           [:a {:href (url-for :account/show {:id id})} "Show"]]
          [:td
           [:a {:href (url-for :account/edit {:id id})} "Edit"]]
          [:td
           (form-for [request :account/destroy {:id id}]
            (submit "Delete"))]])
       account)]]]))

(defn show [request]
  (let [account (account request)
        {} account]
    [:table
     [:tr
      ]
     [:tr
      ]]))

(def params
  (params
    (validates [] :required true)
    (permit [])))

(defn form [request route]
  (let [account (account request)]
    (form-for [request route account]

      (submit "Save"))))

(defn new [request]
  (form request :account/create))

(defn create [request]
  (let [{:db db} request
        result (->> (params request)
                    (insert db :account)
                    (rescue))
        [errors account] result]
    (if (nil? errors)
      (redirect-to :account/index)
      (new (put request :errors errors)))))

(defn edit [request]
  (form request :account/patch))

(defn patch [request]
  (let [{:db db} request
        account (account request)
        result (->> (params request)
                    (update db :account account)
                    (rescue))
        [errors account] result]
    (if (nil? errors)
      (redirect-to :account/index)
      (edit (put request :errors errors)))))

(defn destroy [request]
  (let [{:db db} request
        account (account request)]
    (delete db :account account)
    (redirect-to :account/index)))

which is inline with https://github.com/joy-framework/joy/blob/master/src/joy/cli/routes.txt

ryanford commented 4 years ago

Janet v 1.6.0-da93a73 Joy v 0.5.2

ryanford commented 4 years ago

id is defined locally in the account

swlkr commented 4 years ago

What does your db/schema.sql look like? It looks like the migrations didn't run and there aren't columns in the table

swlkr commented 4 years ago

I did a git diff on my folder and it has this for my db/schema.sql

diff --git a/db/schema.sql b/db/schema.sql
new file mode 100644
index 0000000..4e469b0
--- /dev/null
+++ b/db/schema.sql
@@ -0,0 +1,8 @@
+CREATE TABLE schema_migrations (version text primary key)
+CREATE TABLE account (
+  id integer primary key,
+  email text not null unique,
+  password text not null,
+  created_at integer not null default(strftime('%s', 'now')),
+  updated_at integer
+)
swlkr commented 4 years ago

I'm starting to think this is a linux/macos problem with the sqlite lib 🤔

ryanford commented 4 years ago

You were close. There was no table. I had created an accounts table and an account route. Only error is a user error this time :see_no_evil:

swlkr commented 4 years ago

Oh you know what though... joy should handle plural table names. I have an issue for it here https://github.com/joy-framework/joy/issues/21